On Sun, Jan 5, 2020 at 8:08 AM xap <x...@router.33mail.com> wrote:

> hi, I'm starting out w/ pharo (as my first smalltalk-ish language). am on
> Windows 7 professional; downloaded 64-bit, pharo 8 64-bit development; then
> downgraded to pharo 7, 32-bit stable.
> I went through Prof Stef, and find 1 place of unexpected behavior:
>
> Prof Stef/Conditionals:
>
> "no surprises, prints 100"
> 1 < 2
>   ifTrue: [100]
>   ifFalse: [42].
>
> "i added a, as below, and get an error -- i would've expected a to refer to
> the block, and to be evaluable later, similar to the just prior example in
> ProfStef/Block assignation"
>
> |a|
> a := 1 < 2
>   ifTrue: [:x|x+1]
>   ifFalse: [42].
> a value: 20.
>
> What am I missing, pls?
>

It is always a good idea to describe in detail what behaviour you did
observe. "get an error" is the same as "it didn't work".

It turns out that you are NOT assigning a Block to the temporary variable
a. You are *evaluating* one of the two blocks in the #ifTrue:ifFalse:
expression. Remember that the arguments to #ifTrue: and #ifFalse: are
blocks that will be evaluated if the receiver is the correct Boolean.

In this case, before there is a value to assign to a, the #ifTrue: block
gets evaluated (since 1 is < 2). But, it is a one argument block and there
is no value available for that argument. You should have been given the
opportunity to open the debugger when the error occurred, and you would
have been able to see the stack and recognize that the error occurred in
the execution of the #ifTrue:ifFalse: method.

I won't give you the answer at this point, as you should be able to figure
it out now. Just remember that the expression essentially looks like
aBoolean ifTrue: [anExpression] ifFalse: [anotherExpression]


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

Reply via email to