ifNotNil: perceived strangeness ;)

Ramon Leon ramon.leon at allresnet.com
Wed Oct 3 16:24:23 UTC 2007


> Looking at ProtoObject>>ifNotNil: ifNotNilBlock
> 	"Evaluate the block, unless I'm == nil (q.v.)"
> 
> 	^ ifNotNilBlock valueWithPossibleArgs: {self}ifNotNil:
> 
> 
> ... which is invoked if the receiver is not nil, I would 
> expect the following to work:
> 
> 5 ifNotNil: [:e |  e] to return 5, but what I get is:
> 	
> 	argument of ifNotNil: must be a 0-argument block
> 	
> 	
> However, if first store the block in a temp:
> 
> 	ab := [:e | e]
> 
> 5 ifNotNil: ab.
> 
> 
> returns 5
> 
> I don't understand what the difference is between these; 
> could some kind soul enlighten me as what is happening?
> 
> 
> Cheers,
> 
> Brian

You're looking for ifNotNilDo:[:it | ], ifNotNil:[] does not take an
argument, I believe it's optimized away by the compiler.

Ramon Leon
http://onsmalltalk.com  




More information about the Squeak-dev mailing list