Collection ifEmpty:

Lukas Renggli renggli at gmail.com
Wed Mar 21 08:11:41 UTC 2007


> A bit of collection trivia...
>
> Collection ifEmpty: [ foo ]
> returns nil if the collection is not empty,
> owing to the placement of the return in
>
>         ifEmpty: aBlock
>                 "Evaluate the block if I'm empty"
>                 ^ self isEmpty ifTrue: aBlock

I did the same observation some time ago and found it very irritating
that it does not behave the same was as for example the expression

     ^ var ifNil: [ var := 1 ]

does.

I would vote to change that, so that I can write

    ^ var ifEmpty: [ #( 1 ) ]

to be sure to get a collection with at least one element.

Cheers,
Lukas

-- 
Lukas Renggli
http://www.lukas-renggli.ch



More information about the Squeak-dev mailing list