3 ms·
So the example would be: 1 to: 10. Sends the "to:" message to the number 1 with the parameter 10. Returns a range representing the numbers from 1 to 10.
by mpweiher 9y ago
So the example would be:
1 to: 10.
Sends the "to:" message to the number 1 with the parameter 10. Returns a range representing the numbers from 1 to 10. You can then iterate that range with by sending it the "do:" message with a block argument.
1 to: 10 do:[ :i | <do something with i> ].
And so on. Looks a lot like syntax for a for-loop, but is just message sends to collections with keyword syntax. Every type of collection implements do: (and collect:, select:, reject: etc.) Conditionals work the same way:
1 > 0 ifTrue: [ <do something here> ]
Sends the ifTrue: message to the result of 1 >0. This will be the true object, which is the sole instance of the True class (subclass of Boolean). The True class defines ifTrue: so that it executes the block. Conversely, the False class defines ifTrue: so that it does nothing, ignoring the block.
The fact that what is usually syntax is just message sends means that you can define your own messaging protocols in libraries that also look like syntax.
Dan Ingalls explains: https://youtu.be/P2mh92d-T3Y?t=574 https://youtu.be/P2mh92d-T3Y?t=574
Also, the Grace language expands on this concept by allowing effectively regular expressions of these keyword parameters. Really quite nifty.
- masklinn 9y agoSorry 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
- mpweiher 9y agoFound the paper on generalized method names in Grace: From APIs to languages: generalising method names https://dl.acm.org/citation.cfm?doid=2936313.2816708 https://dl.acm.org/citation.cfm?doid=2936313.2816708 pdf: https://michael.homer.nz/Publications/DLS2015/paper.pdf https://michael.homer.nz/Publications/DLS2015/paper.pdf morning paper: https://blog.acolyer.org/2015/11/09/from-apis-to-languages-generalising-method-names/ https://blog.acolyer.org/2015/11/09/from-apis-to-languages-g...