Login  Register

Re: How does Boolean ifTrue work?

Posted by Sven Van Caekenberghe-2 on Oct 31, 2016; 2:17pm
URL: https://forum.world.st/How-does-Boolean-ifTrue-work-tp4920873p4920889.html

myCollection isEmpty ifTrue: [ self inform: 'The collection is empty' ].

First #isEmpty is sent to myCollection which results in either true or false as value.

Then #ifTrue: is sent to this value.

Now, if the value was true (the sole instance of class True), the code in True>>#ifTrue: is executed, which will evaluate the block by sending it #value. If the value was false, the code in False>>#ifTrue: is executed, and nil is returned and the block is not executed.

Like others said, it is just polymorphism at work.

(And yes, the compiler optimises some of this logic a bit, but the effect is the same).

> On 31 Oct 2016, at 14:58, CodeDmitry <[hidden email]> wrote:
>
> But still, how is the actual argument "alternativeBlock" passed to the
> True/False from a Boolean?
>
> The message does not cache the message inside itself before passing the
> message, and it does not pass the alternative block along with the message.
>
>
>
> --
> View this message in context: http://forum.world.st/How-does-Boolean-ifTrue-work-tp4920873p4920886.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>