Does anyone else feel that
x ifNotNilDo: [: each | Transcript show: each; space]. (1) and x ifNotNil: [ x do: [: each | Transcript show: each; space]]. (2) ought to do the same thing? Or perhaps nil should respond to do: (and do nothing?) The rationale is to be able to use instance variables initialized to nil as empty collections, without having to repeatedly test if they are nil before telling them to do: something, as in phrase (2) above. One might be forgiven for thinking that ifNotNilDo: would have the same effect as do: on a non-nil object. I realize that it is probably infeasible to make a change like this, but I couldn't resist thinking about it. Andrew |
If #do: is to be understood by all objects, then its default behavior should
be to do nothing. The reason is to avoid semantic inconsistency, since "#() do: [:x | Transcript cr; show: x printString]" does not result in anything being displayed on the Transcript. To put it another way: the semantics of #do: are that it enumeratates over the contents/elements of the receiver, specifically excluding the receiver itself. Whether it is wise for all objects to understand #do: is an entirely different issue--one that I will leave as an exercise for others to debate, should they be so inclined. --Alan -----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Andrew P. Black Sent: Friday, April 07, 2006 1:04 PM To: The general-purpose Squeak developers list Subject: (no subject) Does anyone else feel that x ifNotNilDo: [: each | Transcript show: each; space]. (1) and x ifNotNil: [ x do: [: each | Transcript show: each; space]]. (2) ought to do the same thing? Or perhaps nil should respond to do: (and do nothing?) The rationale is to be able to use instance variables initialized to nil as empty collections, without having to repeatedly test if they are nil before telling them to do: something, as in phrase (2) above. One might be forgiven for thinking that ifNotNilDo: would have the same effect as do: on a non-nil object. I realize that it is probably infeasible to make a change like this, but I couldn't resist thinking about it. Andrew |
In reply to this post by Prof. Andrew P. Black
On Apr 7, 2006, at 4:03 PM, Andrew P. Black wrote: > Or perhaps nil should respond to do: (and do nothing?) > Objective-C does something like this: any message sent to nil just answers nil. Whether you prefer that over the Smalltalk strategy of raising MNU for most messages seems to be a matter of taste. Personally, I think either one is viable, but I don't think it would be a good idea to do both. That is, if nil raises MNU, it should do so for almost all messages. Colin |
In reply to this post by Prof. Andrew P. Black
Am 07.04.2006 um 22:03 schrieb Andrew P. Black: > Does anyone else feel that > > x ifNotNilDo: [: each | Transcript show: each; space]. (1) > > and > > x ifNotNil: [ x do: [: each | Transcript show: each; space]]. (2) > > ought to do the same thing? > > Or perhaps nil should respond to do: (and do nothing?) > > The rationale is to be able to use instance variables initialized > to nil as empty collections, without having to repeatedly test if > they are nil before telling them to do: something, as in phrase (2) > above. One might be forgiven for thinking that ifNotNilDo: would > have the same effect as do: on a non-nil object. > > I realize that it is probably infeasible to make a change like > this, but I couldn't resist thinking about it. Agreed, it would more aptly named ifNotNilIn:, as it is equivalent to "x ifNotNil: [x in: aBlock]". Your problem reminds me of a pattern I've used before, not sure of its "official" name. It optimizes collections of 0 and 1 elements: x := nil. "empty list" x myDo: [:each | ...]. "not evaluated" x := x myAdd: 42. "x = 42" x myDo: [:each | ...]. "evaluated once" x := x myAdd: 'hi'. "x = #(42 'hi')" x myDo: [:each | ...]. "evaluated twice" - Bert - |
Free forum by Nabble | Edit this page |