<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: NotNil, then what?</title>
	<atom:link href="http://blog.3plus4.org/2007/04/22/notnil-then-what/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.3plus4.org/2007/04/22/notnil-then-what/</link>
	<description>The neighbourhood of 7</description>
	<pubDate>Sat, 19 Jul 2008 08:06:29 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Peter William Lount</title>
		<link>http://blog.3plus4.org/2007/04/22/notnil-then-what/#comment-45</link>
		<dc:creator>Peter William Lount</dc:creator>
		<pubDate>Tue, 24 Apr 2007 03:08:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.3plus4.org/2007/04/22/notnil-then-what/#comment-45</guid>
		<description>ON SECOND THOUGHT
Oh, on second thought the last example given above, "foo ifNil: [foo := false] ifTrue: [ … ] ifFalse: [ … ]" isn't the best example for "lazy initialization" since if the value of "foo" is nil then the "ifNil:" block is likely to fill in either "true" or "false"; but what happens then? In the normal way it's written in Smalltalk (as shown above) it would be something like this:

     (foo ifNil: [foo := false]) ifTrue: [ … ] ifFalse: [ … ].

This means that for 

     foo ifNil: [foo := false] ifTrue: [ … ] ifFalse: [ … ]

to work the same way, the following would need to be implemented something like this.

     !UndefinedObject methods!
          ifNil: nilBlock ifTrue: trueBlock ifFalse: falseBlock
	     ^nilBlock value ifTrue: trueBlock ifFalse: falseBlock

Of course if this control structure isn't used for "lazy initialization" the following could be the implementation on UndefinedObject which simply evaluates the nil block and returns; naturally true and false would be implemented in a similar manner. This is the normal expected way of doing this sort of protocol in Smalltalk.

     !UndefinedObject methods!
          ifNil: nilBlock ifTrue: trueBlock ifFalse: falseBlock
	     ^nilBlock value

Refining the more flexible version to handle both scenarios better:

     !UndefinedObject methods!
          ifNil: nilBlock ifTrue: trueBlock ifFalse: falseBlock
	           &#124; result &#124;
		     result := nilBlock value.
		     result ifNil: [^self] ifNotNil: [
			     result == true ifTrue: [^ trueBlock value ].
			     result == false ifTrue: [^ falseBlock value ]
		     ].
		     ^result

Not the nicest implementation but it gets the job done for the "normal case" and the "lazy initialization" case. Now if after the lazy initialization it's true or false one of those two true/false blocks will get executed, otherwise the value is simply returned. Of course we can, and likely would want for some cases, the "ifNotNil:" keyword added to the protocol pattern; like so:

     !UndefinedObject methods!
          ifNil: nilBlock ifNotNil: notNilBlock ifTrue: trueBlock ifFalse: falseBlock
	           &#124; result &#124;
		     result := nilBlock value.
		     ^ result ifNil: [self] ifTrue: [ trueBlock ] ifFalse: [ falseBlock ]

     !Object methods!
          ifNil: nilBlock ifNotNil: notNilBlock ifTrue: trueBlock ifFalse: falseBlock
	          ^ notNilBlock isOneArgumentBlock 
                       ifTrue: [ notNilBlock value: self ]
                       ifFalse: [ notNilBlock value ]

    !True methods!
          ifNil: nilBlock ifNotNil: notNilBlock ifTrue: trueBlock ifFalse: falseBlock
	          ^ trueBlock value

    !False methods!
          ifNil: nilBlock ifNotNil: notNilBlock ifTrue: trueBlock ifFalse: falseBlock
	          ^ falseBlock value

Ok, not perfect but it covers the cases mentioned above. With a set of sunit tests to ensure that it works as expected in all the cases we can begin to use it throughout code for "lazy initialization" scenarios plus others. Naturally It could also be re-implemented with a double dispatch.

However, something is nagging me about this protocol... that is that unlike other similar protocols in Smalltalk which simply "branch" on the receiver (ifTrue:, ifFalse:, ifNil:, ifNotNil:) this one recomputes when the receiver is nil and then branches based on the receiver of the re-computation. This changes the basic assumptions of the normal protocols which programmers might come to expect as normal. Sometimes it's best to not challenge these basic assumptions too much as it could be a source of bugs due to misinterpretations; after all our goal is to make coding safer and simpler, not more complex or obscure. (If you really want more obscure and complex coding use a language such as Perl which implements new features in language syntax rather than in an extensible library).

So while this is an interesting protocol I'd likely not actually use it in practice. So it's not been a very good example after all; however, it's been interesting in that not all experiments or attempts to create new control structures for Smalltalk work out in practice. Maybe later I'll work out another alternative, or maybe not. The point is that Smalltalk allows for these experiments to be carried out without changing the language syntax! It means that Smalltalk can adapt to your needs and the needs of your application; and isn't that what software is for?

APPRECIATING the BEAUTY OF "OBJECT PASS THROUGH" in PROTOCOLS
Taking another look at this:

      (foo ifNil: [foo := false]) ifTrue: [ … ] ifFalse: [ … ]

we can see that there are a number of possible cases: foo is nil, foo is true, foo is false, foo is some other object. If foo is some other object (other than a boolean) it will be passed through "ifNil:" - in this case we'll get an error since most objects don't understand ifTrue:ifFalse. However, we're expecting foo to be true or false for this variable; and the lazy initializer sets it up for us with "(foo ifNil: [foo := false])". What's interesting about "ifNil:" is that if foo isn't nil the "ifNil:" simply returns the receiver by passing it through the "ifNil:" message! This allows the subsequent messages to be sent to the receiver, which in this case is foo, whether or not it's been initialized lazily here. The same works for "ifNotNil:" in the case of "nil"; for example:
     
     (someObject ifNotNil: [ ... ]) someMessage.

If someObject is nil the message "someMessage" will be sent to nil, and if someObject is a non nil object of some kind the block will execute returning it's object for "someMessage" to be sent to.

When constructing your own control structure protocols it's important to choose which objects are returned from the protocol for each case. This allows for compact beauty. Beauty and simplicity enable power of expression</description>
		<content:encoded><![CDATA[<p>ON SECOND THOUGHT<br />
Oh, on second thought the last example given above, &#8220;foo ifNil: [foo := false] ifTrue: [ … ] ifFalse: [ … ]&#8221; isn&#8217;t the best example for &#8220;lazy initialization&#8221; since if the value of &#8220;foo&#8221; is nil then the &#8220;ifNil:&#8221; block is likely to fill in either &#8220;true&#8221; or &#8220;false&#8221;; but what happens then? In the normal way it&#8217;s written in Smalltalk (as shown above) it would be something like this:</p>
<p>     (foo ifNil: [foo := false]) ifTrue: [ … ] ifFalse: [ … ].</p>
<p>This means that for </p>
<p>     foo ifNil: [foo := false] ifTrue: [ … ] ifFalse: [ … ]</p>
<p>to work the same way, the following would need to be implemented something like this.</p>
<p>     !UndefinedObject methods!<br />
          ifNil: nilBlock ifTrue: trueBlock ifFalse: falseBlock<br />
	     ^nilBlock value ifTrue: trueBlock ifFalse: falseBlock</p>
<p>Of course if this control structure isn&#8217;t used for &#8220;lazy initialization&#8221; the following could be the implementation on UndefinedObject which simply evaluates the nil block and returns; naturally true and false would be implemented in a similar manner. This is the normal expected way of doing this sort of protocol in Smalltalk.</p>
<p>     !UndefinedObject methods!<br />
          ifNil: nilBlock ifTrue: trueBlock ifFalse: falseBlock<br />
	     ^nilBlock value</p>
<p>Refining the more flexible version to handle both scenarios better:</p>
<p>     !UndefinedObject methods!<br />
          ifNil: nilBlock ifTrue: trueBlock ifFalse: falseBlock<br />
	           | result |<br />
		     result := nilBlock value.<br />
		     result ifNil: [^self] ifNotNil: [<br />
			     result == true ifTrue: [^ trueBlock value ].<br />
			     result == false ifTrue: [^ falseBlock value ]<br />
		     ].<br />
		     ^result</p>
<p>Not the nicest implementation but it gets the job done for the &#8220;normal case&#8221; and the &#8220;lazy initialization&#8221; case. Now if after the lazy initialization it&#8217;s true or false one of those two true/false blocks will get executed, otherwise the value is simply returned. Of course we can, and likely would want for some cases, the &#8220;ifNotNil:&#8221; keyword added to the protocol pattern; like so:</p>
<p>     !UndefinedObject methods!<br />
          ifNil: nilBlock ifNotNil: notNilBlock ifTrue: trueBlock ifFalse: falseBlock<br />
	           | result |<br />
		     result := nilBlock value.<br />
		     ^ result ifNil: [self] ifTrue: [ trueBlock ] ifFalse: [ falseBlock ]</p>
<p>     !Object methods!<br />
          ifNil: nilBlock ifNotNil: notNilBlock ifTrue: trueBlock ifFalse: falseBlock<br />
	          ^ notNilBlock isOneArgumentBlock<br />
                       ifTrue: [ notNilBlock value: self ]<br />
                       ifFalse: [ notNilBlock value ]</p>
<p>    !True methods!<br />
          ifNil: nilBlock ifNotNil: notNilBlock ifTrue: trueBlock ifFalse: falseBlock<br />
	          ^ trueBlock value</p>
<p>    !False methods!<br />
          ifNil: nilBlock ifNotNil: notNilBlock ifTrue: trueBlock ifFalse: falseBlock<br />
	          ^ falseBlock value</p>
<p>Ok, not perfect but it covers the cases mentioned above. With a set of sunit tests to ensure that it works as expected in all the cases we can begin to use it throughout code for &#8220;lazy initialization&#8221; scenarios plus others. Naturally It could also be re-implemented with a double dispatch.</p>
<p>However, something is nagging me about this protocol&#8230; that is that unlike other similar protocols in Smalltalk which simply &#8220;branch&#8221; on the receiver (ifTrue:, ifFalse:, ifNil:, ifNotNil:) this one recomputes when the receiver is nil and then branches based on the receiver of the re-computation. This changes the basic assumptions of the normal protocols which programmers might come to expect as normal. Sometimes it&#8217;s best to not challenge these basic assumptions too much as it could be a source of bugs due to misinterpretations; after all our goal is to make coding safer and simpler, not more complex or obscure. (If you really want more obscure and complex coding use a language such as Perl which implements new features in language syntax rather than in an extensible library).</p>
<p>So while this is an interesting protocol I&#8217;d likely not actually use it in practice. So it&#8217;s not been a very good example after all; however, it&#8217;s been interesting in that not all experiments or attempts to create new control structures for Smalltalk work out in practice. Maybe later I&#8217;ll work out another alternative, or maybe not. The point is that Smalltalk allows for these experiments to be carried out without changing the language syntax! It means that Smalltalk can adapt to your needs and the needs of your application; and isn&#8217;t that what software is for?</p>
<p>APPRECIATING the BEAUTY OF &#8220;OBJECT PASS THROUGH&#8221; in PROTOCOLS<br />
Taking another look at this:</p>
<p>      (foo ifNil: [foo := false]) ifTrue: [ … ] ifFalse: [ … ]</p>
<p>we can see that there are a number of possible cases: foo is nil, foo is true, foo is false, foo is some other object. If foo is some other object (other than a boolean) it will be passed through &#8220;ifNil:&#8221; - in this case we&#8217;ll get an error since most objects don&#8217;t understand ifTrue:ifFalse. However, we&#8217;re expecting foo to be true or false for this variable; and the lazy initializer sets it up for us with &#8220;(foo ifNil: [foo := false])&#8221;. What&#8217;s interesting about &#8220;ifNil:&#8221; is that if foo isn&#8217;t nil the &#8220;ifNil:&#8221; simply returns the receiver by passing it through the &#8220;ifNil:&#8221; message! This allows the subsequent messages to be sent to the receiver, which in this case is foo, whether or not it&#8217;s been initialized lazily here. The same works for &#8220;ifNotNil:&#8221; in the case of &#8220;nil&#8221;; for example:</p>
<p>     (someObject ifNotNil: [ ... ]) someMessage.</p>
<p>If someObject is nil the message &#8220;someMessage&#8221; will be sent to nil, and if someObject is a non nil object of some kind the block will execute returning it&#8217;s object for &#8220;someMessage&#8221; to be sent to.</p>
<p>When constructing your own control structure protocols it&#8217;s important to choose which objects are returned from the protocol for each case. This allows for compact beauty. Beauty and simplicity enable power of expression</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter William Lount</title>
		<link>http://blog.3plus4.org/2007/04/22/notnil-then-what/#comment-44</link>
		<dc:creator>Peter William Lount</dc:creator>
		<pubDate>Mon, 23 Apr 2007 13:24:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.3plus4.org/2007/04/22/notnil-then-what/#comment-44</guid>
		<description>As one of those early adopters of ifNil:ifNotNil: (and all of it's variants) I support and applaud the enhancement that you are suggesting. However, it's important that the block actually have the option for zero or one arguments as this allows us to have "stand in" polymorphic objects which pretend to be blocks (via implementation of the minimal block protocol).

It's the one of the first adjustments which I always add to any Smalltalk when I start using it. Recently I added it to Susie Smalltalk.

UNIFORMITY+SYMMETRY IN PROTOCOLS
It's actually useful in a number of limited cases to allow a parameter for "ifNil:" for uniformity purposes. For example, when the block is being passed around to various statements this can save a lot of other coding. The value passed into the ifNil: block is, of course, nil.

     self foo ifNil: aBlock.
     self bar ifNotNil: aBlock.

It makes ifNil: and ifNotNil: uniform and symmetric in terms of their calling usage with blocks. It's makes them interchangeable. Another example where the message selector is either "ifNil:" or "ifNotNil:" is in "plug and play" of components.

     self perform: aMessageSelector with: aBlock.

While ifNil: and ifNotNil: are asymmetric in semantics (as pointed out above in the article) they can be made uniform and symmetric in their message sending usage.

STANDARDIZATION: Smalltalk 2010
I think we should propose ifNil:, ifNotNil:, ifNil:ifNotNil:, and ifNotNil:ifNil: with or without a one argument block become part of the Smalltalk standard.

Years ago I added these to VisualWorks while working on a very large project at a Wall Street company. Six months later I was instructed to remove them and convert all the hundreds of senders to the "isNil ifTrue:ifFalse:" semantic for a few reasons: 1) it was an addition to a base class and they didn't want to do that; 2) it wasn't "standard Smalltalk" and they didn't like to read it; and, 3) it was slower than the alternative. All the arguments (which you have expressed above) for this ifNil: enhancement were tossed aside. I'm glad the times have changed.

BEYOND THE FUTURE
Now to push the future further. How about this useful tidbit (and it's other variations) that's helpful in dealing with those pesky situations when you really don't know what's coming back from "foo".

self foo ifNil: [ ... ] ifNotNil: [:aReceiver &#124; ... ] ifTrue: [ ... ] ifFalse: [ ... ].

For example "ifNil:ifTrue:ifFalse:" is useful for lazy initialization.

foo ifNil: [foo := false] ifTrue: [ ... ] ifFalse: [ ... ].

The above replaces the following. It might not seem much different but it is less verbose.

(foo ifNil: [foo := false]) ifTrue: [ ... ] ifFalse: [ ... ].

One of the powerful advantages of Smalltalk syntax is that control structures are implemented in the object library and anyone can add new and useful control structures. Another powerful advantage of "pre-canned" control structures is that once they are tested (using something like sunit) they can increase the reliability of the code which uses them; since it's less likely for mistakes to be made inlining and reproducing the logic of the control structure each time.</description>
		<content:encoded><![CDATA[<p>As one of those early adopters of ifNil:ifNotNil: (and all of it&#8217;s variants) I support and applaud the enhancement that you are suggesting. However, it&#8217;s important that the block actually have the option for zero or one arguments as this allows us to have &#8220;stand in&#8221; polymorphic objects which pretend to be blocks (via implementation of the minimal block protocol).</p>
<p>It&#8217;s the one of the first adjustments which I always add to any Smalltalk when I start using it. Recently I added it to Susie Smalltalk.</p>
<p>UNIFORMITY+SYMMETRY IN PROTOCOLS<br />
It&#8217;s actually useful in a number of limited cases to allow a parameter for &#8220;ifNil:&#8221; for uniformity purposes. For example, when the block is being passed around to various statements this can save a lot of other coding. The value passed into the ifNil: block is, of course, nil.</p>
<p>     self foo ifNil: aBlock.<br />
     self bar ifNotNil: aBlock.</p>
<p>It makes ifNil: and ifNotNil: uniform and symmetric in terms of their calling usage with blocks. It&#8217;s makes them interchangeable. Another example where the message selector is either &#8220;ifNil:&#8221; or &#8220;ifNotNil:&#8221; is in &#8220;plug and play&#8221; of components.</p>
<p>     self perform: aMessageSelector with: aBlock.</p>
<p>While ifNil: and ifNotNil: are asymmetric in semantics (as pointed out above in the article) they can be made uniform and symmetric in their message sending usage.</p>
<p>STANDARDIZATION: Smalltalk 2010<br />
I think we should propose ifNil:, ifNotNil:, ifNil:ifNotNil:, and ifNotNil:ifNil: with or without a one argument block become part of the Smalltalk standard.</p>
<p>Years ago I added these to VisualWorks while working on a very large project at a Wall Street company. Six months later I was instructed to remove them and convert all the hundreds of senders to the &#8220;isNil ifTrue:ifFalse:&#8221; semantic for a few reasons: 1) it was an addition to a base class and they didn&#8217;t want to do that; 2) it wasn&#8217;t &#8220;standard Smalltalk&#8221; and they didn&#8217;t like to read it; and, 3) it was slower than the alternative. All the arguments (which you have expressed above) for this ifNil: enhancement were tossed aside. I&#8217;m glad the times have changed.</p>
<p>BEYOND THE FUTURE<br />
Now to push the future further. How about this useful tidbit (and it&#8217;s other variations) that&#8217;s helpful in dealing with those pesky situations when you really don&#8217;t know what&#8217;s coming back from &#8220;foo&#8221;.</p>
<p>self foo ifNil: [ ... ] ifNotNil: [:aReceiver | ... ] ifTrue: [ ... ] ifFalse: [ ... ].</p>
<p>For example &#8220;ifNil:ifTrue:ifFalse:&#8221; is useful for lazy initialization.</p>
<p>foo ifNil: [foo := false] ifTrue: [ ... ] ifFalse: [ ... ].</p>
<p>The above replaces the following. It might not seem much different but it is less verbose.</p>
<p>(foo ifNil: [foo := false]) ifTrue: [ ... ] ifFalse: [ ... ].</p>
<p>One of the powerful advantages of Smalltalk syntax is that control structures are implemented in the object library and anyone can add new and useful control structures. Another powerful advantage of &#8220;pre-canned&#8221; control structures is that once they are tested (using something like sunit) they can increase the reliability of the code which uses them; since it&#8217;s less likely for mistakes to be made inlining and reproducing the logic of the control structure each time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vassili Bykov</title>
		<link>http://blog.3plus4.org/2007/04/22/notnil-then-what/#comment-43</link>
		<dc:creator>Vassili Bykov</dc:creator>
		<pubDate>Mon, 23 Apr 2007 09:06:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.3plus4.org/2007/04/22/notnil-then-what/#comment-43</guid>
		<description>I noticed ifNotNilDo:, but to me coming from using dual-choice ifNotNil: in VisualWorks and monadic one (or was it dual-choice too?) in Squeak of '99 vintage, it seems like an unfortunate step away from a de facto standard that was beginning to form. That's when some people feel that ifNil:/NotNil: alone are unnecessarily bloating the core library. Thank you for the pointer to bugs.squeak.org. There appears to be a fresh issue 0006426 discussing exactly this.</description>
		<content:encoded><![CDATA[<p>I noticed ifNotNilDo:, but to me coming from using dual-choice ifNotNil: in VisualWorks and monadic one (or was it dual-choice too?) in Squeak of &#8216;99 vintage, it seems like an unfortunate step away from a de facto standard that was beginning to form. That&#8217;s when some people feel that ifNil:/NotNil: alone are unnecessarily bloating the core library. Thank you for the pointer to bugs.squeak.org. There appears to be a fresh issue 0006426 discussing exactly this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lukas Renggli</title>
		<link>http://blog.3plus4.org/2007/04/22/notnil-then-what/#comment-42</link>
		<dc:creator>Lukas Renggli</dc:creator>
		<pubDate>Mon, 23 Apr 2007 05:32:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.3plus4.org/2007/04/22/notnil-then-what/#comment-42</guid>
		<description>I don't know if you noticed that there is #ifNotNilDo: to avoid a double evaluation of the receiver and to get the receiver into the block as an argument. Unfortunately #ifNotNilDo: is not inlined. Your approach is certainly very nice and efficient, though I am not entirely sure if it is good design to have a message accept zero- and one-argument blocks. Still, I would like to suggest that you submit your change to http://bugs.squeak.org so that it can be discussed and maybe included into Squeak 3.10.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know if you noticed that there is #ifNotNilDo: to avoid a double evaluation of the receiver and to get the receiver into the block as an argument. Unfortunately #ifNotNilDo: is not inlined. Your approach is certainly very nice and efficient, though I am not entirely sure if it is good design to have a message accept zero- and one-argument blocks. Still, I would like to suggest that you submit your change to <a href="http://bugs.squeak.org" rel="nofollow">http://bugs.squeak.org</a> so that it can be discussed and maybe included into Squeak 3.10.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
