Sunday, June 11, 2006

Cool Block Idea

Howard Oh had the following suggestion on comp.lang.smalltalk.dolphin:

Block is one of the reasons that makes Smalltalk so lovable. But sometimes, block can make expressions very hard to read, because we human read parameter object first and then read inside the block just like Smalltalk runtime would do. This makes our reading cursor jump to right end and then back to left.

A guick example that can depict this point can be...
[:x| x sqrt ] value: ([:x| x + 5 ] value: ([:x| x * x ] value: 2))

An idea came up to me a few minutes ago,

Define a method for Object
Object>>--> aBlock

^aBlock value: self

This new method can transform the example expression to...
2 --> [:x| x * x ] --> [:x| x + 5 ] --> [:x| x sqrt ] 


What a cool idea! How I love messages and closures!

No comments: