Collection ifEmpty:

Andreas Raab andreas.raab at gmx.de
Wed Mar 21 08:19:34 UTC 2007


brad fowlow wrote:
>     ifEmpty: aBlock
>         "Evaluate the block if I'm empty"
>         ^ self isEmpty ifTrue: aBlock
> 
> 
> It would be a tad less efficient,
> but (I think) more useful to say
>     ifEmpty: aBlock
>         self isEmpty ifTrue: [ ^ aBlock value ]

Without any doubt, the latter version is what it's meant to be. The 
former makes no sense (or rather: as much or as little sense as 
Object>>ifNil: would make returning nil when the receiver is non-nil).

Besides, the latter is *more* efficient. That's because ifTrue: gets 
inlined in this case but not in the former requiring a real send before 
activation of the block whereas in the second version (due to inlining) 
no send occurs for ifTrue: (look at the bytecodes to see the difference).

Cheers,
   - Andreas




More information about the Squeak-dev mailing list