ifNil, ifNotNil

Eric Arseneau eat at huv.com
Thu Aug 3 16:18:49 UTC 2000


I hate to bring an old subject up, but I've just hit it.

I found an old discussion on the form on this subject and would like to
bring a different perspective.

a := anObject ifNil: ['Hello']

Is an expression that makes sense and is desirable.  The result of this
expression is anObject if anObject is not nil, and 'Hello' if anObject is
nil.  It allows me to write some very simple expressions without having to
do something like.

a := anObject.
a isNil ifTrue: [
	a := 'Hello'].

But the real reason I bring this up is do to an old post I think I made a
few weeks ago, and I can't find it in the archive.  I've just run into a
problem where the parser is telling me that ifNotNil: expects a zero
argument block.  I added support for 0 or 1 argument blocks for this in the
2.7 I was using.  Now I look in 2.8 and find that the ifNil, ifNotNil and
variants get transformed and do not support the argument case.  Support an
argument to ifNotNil: is VERY useful.  Think of the following:

a := self someLongOrExpensiveOperation ifNotNil: [:value | value
someThingIWantOutOfTheResult].
a isNil ...

What the above allows me to do is to not need to define a temp.  This
becomes even more important when we starting having real block closures.
When this happens, the block that is for the ifNotNil is of ZERO cost for
the nil case, and cheap otherwise.

Is there any problem in adding support for zero and one argument blocks for
ifNotNil ???





More information about the Squeak-dev mailing list