Message Eating Null - article

Vassili Bykov smalltalkbigot at gmail.com
Thu Jul 26 20:28:38 UTC 2007


I think proper exception handling combined with reflection can produce
a very readable result without the flakiness of message-eating nil:

    lastNumber := [person office phone lastNumberDialed asString]
ifNilShowsUp: ['']

with #ifNilShowsUp: defined as

    BlockContext>>ifNilShowsUp: aBlock
	^self
		on: MessageNotUnderstood
		do:
			[:ex |
			(ex receiver isNil and: [ex signalerContext sender = self])
				ifTrue: [ex return: aBlock value]
				ifFalse: [ex pass]]

Or in other words, the second block is evaluated to produce the final
result if one of the messages inside the first block returns nil and
that nil doesn't understand the following message. All other failures,
including MNUs by nil inside the messages sent from the block, fail
"properly".

Cheers,

--Vassili

P.S. "ex signalerContext sender = self" would fail to capture a
relevant MNU in some cases. The 100% solution is an exercise for the
reader. :)



More information about the Squeak-dev mailing list