Your problem has to do with understanding how the 'ifTrue:ifFalse:' message
is evaluated.

This expression
    1 < 2
      ifTrue: [100]
      ifFalse: [42].

evaluates to '100'; it does not evaluate to "a block that will return 100
when evaluated".  

The blocks that make up the arguments to 'ifTrue:ifFalse:' will be
selectively evaluated at run-time, by the receiver of the message, when the
receiver makes the determination of which block to evaluate.  In your
modification, you mistakenly assume that one of the two block arguments will
selected and returned verbatim (to be evaluated later in another context),
but this is not how it works.

When you write 'ifTrue: [:x|x+1]', and the receiver of the message is
'True', then 'True' will attempt to evaluate your block argument.  But your
block argument requires an argument itself, which is missing -- Hence the
error that results.

What you want is an "ifTrue:' argument block that will itself evaluate to
another block that you can assign to 'a'.  The fix is simple: Just enclose
it in another set of '[ ]' like so:

    |a|
    a := 1 < 2
      ifTrue: [ [:x|x+1] ]
      ifFalse: [42].
    a value: 20. 

And the result will be '21', as intended.

-Ted






--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply via email to