Monday 2 November 2015

Blocks and how to apply them to composition of a variety of GUI buttons

To define a single button class and have several buttons
Each button has its own text, and it must also alert the owner for a change of state
Done by inheritance, it would get complicated

So:
Button new text: 'some text'; action: [ do something ]

The button may act as a switch or single action.  If it's a switch it will have to alert the owner to disable the other options

For those are just instance-specific actions, you don't need several button classes.

Instead:
switchButton := Button new action: [ self owner switch ].
anotherButton := Button new action: [ self owner whatever ].

[ 1 + 1 ] 
results in the block
[ 1 + 1 ]

To execute a block we pass it the getter message value  When evaluated, a block will by default return the value of the last expression it evaluates.

[ 1 + 1 ] value 
results in 2

To pass a block parameters, we use the setter message value:value:value:
[ :a :b | a + b ] value: 1 value: 2
results in 3

No comments:

Post a Comment