4 ms·
"smalltalk cutest feature is the named parameter syntax trick..." make-range from: 10 to: 20 calss (make-range.from:to 10 20) or something similar. Free nano
by pmoriarty 9y ago
"smalltalk cutest feature is the named parameter syntax trick..."
make-range from: 10 to: 20 calss (make-range.from:to 10 20) or something similar. Free nanodsls
For the uninitiated among us, what does that do and what's so magic about it?
- mathieuh 9y agoYou can put the parameter names in method calls, which makes for much more readable code. Like a makeRange method may take parameters `from` and `to`, when calling this method you could do `makeRange(from: 0, to:10)`. That’s a fairly trivial example but it demonstrates how readability is improved. You can imagine how this becomes even more useful with methods that take many parameters. An interesting document to look at regarding this is Swift’s guidelines on naming methods.
- pmoriarty 9y ago"You can put the parameter names in method calls, which makes for much more readable code. Like a makeRange method may take parameters `from` and `to` ... That’s a fairly trivial example but it demonstrates how readability is improved." Is that something like keyword arguments in Lisp?
- mbrock 9y agoExcept Smalltalk keywords can't be reordered or omitted. It's more like you put the arguments in the middle of the method name, like a kind of mixfix notation. The method name would be "rangeFrom:to:withStep:" and that means you call it exactly like that, with arguments after the colon. Objective C is the same.
- mpweiher 9y ago> makeRange(from: 0, to:10) [..] Swift’s guidelines on naming methods. The syntax you show is Swift, which is a weird mash-up of C/C++ style function call/method call syntax with keywords jammed into them. To me, actual Smalltalk syntax is just so much cleaner: 0 to: 10. It manages the "reads like english" trick without trying to be english-like or natural-language-like. (See Applescript for the disaster that results from that: "Many of the current problems in AppleScript can be traced to the use of syntax based on natural language" http://www.cs.utexas.edu/~wcook/Drafts/2006/ashopl.pdf http://www.cs.utexas.edu/~wcook/Drafts/2006/ashopl.pdf).
- Aaargh20318 9y agoAppleScript may be the only read-only programming language in existence.
- sethrin 9y agoDepending on your definition of programming language, that crown might actually go to HAGGIS. It's an "educational" programming language which is not executable (or intended to be). It's possibly the worst idea in the history of computing, and that it is taught to anyone is (IMO) a public offense. https://en.wikipedia.org/wiki/HAGGIS https://en.wikipedia.org/wiki/HAGGIS
- justbaker 9y ago"ah, scary, close tab!"
- dTal 9y agoI get that vibe from Inform 7: the famous Dijkstra’s algorithm reads like English, but I'd have no idea how to write such a program.
- throwaway7645 9y agoInform7 is mostly for text adventures. I'm sure it's not bad if you give it a go as many people with little to no coding experience write expansive text games using it.
- mpweiher 9y agoI am still waiting for the combination of Perl and Applescript. You have to write it in Applescript, but you get to read it only in transliterated Perl.
- mpweiher 9y agoSo 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 ;)
- SolarNet 9y agoIt's keywords as sentences. It basically allows a function signature to be spelled out as a sentence with infix operators. These days you can see the lineage of smalltalk in keyword arguments. For example, python vs. smalltalk: obj.do_thing("string", with_factor=8, log_to=logger) obj do_thing: "string" with_factor: 8 log_to: logger One of those reads as a sentence a little better. But to be fair python learned this from languages that had already taken this idea from smalltalk. Like most older languages smalltalk's implementation is more powerful than the bastardization that other languages learned from it. Library interfaces in smalltalk are built around this syntax feature. Note how the function call looks the same as the keyword argument. This shared syntax makes the language read better. It makes function calls and keywords look like a normal infix language (e.g. the math expressions in most languages). Where parens are used like any other infix language (rather than be overloaded for function calls): (obj do_thing: "string" with_factor: 8) log_to: logger would then call ".log_to(logger)" on the result of the parenthesized expression. The same way in (2 + 4) * 5 multiplies the result of the parens by 5. A single syntax structure for all these concepts - rather than mixing multiple syntax - allows it to express new ideas in the same syntax as everything else. Like lisp does.
- kbp 9y agoIn Smalltalk, the 'keywords' are actually part of the method name; when you say '1 to: 10 do: [ ... ]', you're not calling to: on 1 (which is an entirely different method) with a keyword argument, you're calling to:do:. > This shared syntax makes the language read better. It makes function calls and keywords look like a normal infix language (e.g. the math expressions in most languages). Ironically, since Smalltalk also applies this uniformity to arithmetic operators, they don't behave how most people would expect: a + b * c - d is executed like (((a + b) * c) - d).
- SolarNet 9y agoYea I was eliding some points to try and explain it better.