4 ms·
Sorry to "well actually" you, but 1 to: 10 do:[ :i | <do something with i> ]. would be a single message `to:do:`, not a chain of messages.
by masklinn 9y ago
Sorry to "well actually" you, but
1 to: 10 do:[ :i | <do something with i> ].
would be a single message `to:do:`, not a chain of messages.
- astrobe_ 9y agowell actually-actually :-), (1 to: 10) do: [ :i | Transcript show: i ; cr]. also works in Pharo. Both are valid because: "to:do:" is a message understood by the Number class "to:" is a message also understood by the Number class that yields an Interval object, which understands the "do:" message.
- masklinn 9y ago> well actually-actually :-), I didn't say you could not build a message chain to do that, only that to:do: as shown in mpweiher's original comment is a single message ;)
- astrobe_ 9y agoYeah. My reply was more meant to show off more Smalltalk to pmoriarty and others that to one-up everyone :-)
- mpweiher 9y agoYes, I left that detail out for pedagogical purposes. As the other respondent wrote, it still works the same way but with parentheses, and the to:do: exists as a convenience. In Objective-Smalltalk[1], I added | so you can do this without parenthesis: (1 to:10) do: [ :i | stdout println:i. ]. can become: 1 to:10 | do: [ :i | stdout println:i ]. This is nice when you are constructing a statement interactively, for example on a command line and don't want to go back. It also makes things less convoluted when the chain gets longer. It also "fixes" the asymmetry that chaining works with unary messages but not with keyword messages. Of course, the whole thing becomes even more elegant, IMHO, with a Higher Order Message (HOM)[2]: stdout do println: (1 to:10) each. YMMV. [1] http://objective.st/ http://objective.st/ [2] https://en.wikipedia.org/wiki/Higher_order_message https://en.wikipedia.org/wiki/Higher_order_message