4 ms·
Smalltalk's "cascading" operator is a strange (in the nicest way possible, it's mostly strange that nobody else has it) feature: a pretty popular style in OO la
by masklinn 15y ago
Smalltalk's "cascading" operator is a strange (in the nicest way possible, it's mostly strange that nobody else has it) feature: a pretty popular style in OO languages these days is "fluent interfaces", in which methods return their `this` and — through method chaining — developers can call several methods on the same object while limiting verbosity. This style is of course possible in smalltalk:
foo.bar().baz()
becomes
foo bar baz
and if `bar` returns `self`/`this`, `baz` will be called on the same object. However, this has the issue that method implementors have to choose between fluent interfaces and returning useful values, as the fluency is decided by the return value domain for the method.
Smalltalk sidesteps this by having an operator `;` which essentially means "the message after this operator is sent to the same object as the message before this operator". As a result, in the example above you could have `bar` returning the height of the empire state building, and you'd still get the exact same result by writing:
foo bar ; baz
(a message cascade returns the value returned by the last message)
Smalltalk was/is not fond of literal syntaxes, so its collection APIs (as far as adding to them goes anyway) look a lot like Java's, yet you could write things in a terser manner by using cascading:
c := (ArrayList new) add: 3
; add: 4
; add: 5
; add: 6
; yourself.
is equivalent to:
c := ArrayList new.
c add: 3.
c add: 4.
c add: 5.
c add: 6.
(note `yourself`, which does exactly what it says: it returns its `self`)
(and yes I know you can combine Java's anonymous subclasses and instance initializers to get something similar, in the specific case of instantiating non-final collections).
- munificent 15y ago> it's mostly strange that nobody else has it For what it's worth, we're working on adding it to Dart. I don't think it's a done deal yet, but I believe we're close to having something finalized.
- bokchoi 15y ago> it's mostly strange that nobody else has it It looks like it is the same as clojure's doto macro: http://clojuredocs.org/clojure_core/clojure.core/doto http://clojuredocs.org/clojure_core/clojure.core/doto And it is strange more languages don't have it.
- masklinn 15y agoIt is pretty nice (and makes Clojure jump up a place or two in the completely unranked list of "languages I really need to learn"). It looks very similar yes, although there's a difference in that `doto` can only returns its `this`.
- ajuc 15y agoClojure has something even cooler than doto. It's called "thrush operators": http://clojuredocs.org/clojure_core/clojure.core/-%3E http://clojuredocs.org/clojure_core/clojure.core/-%3E and http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E
- Someone 15y ago'with' (Pascal, MS Visual Basic, possibly others) is somewhat equivalent (it lacks the ability to return self) but more explicit: c := LeftAsAnExerciseForTheReader(); with c do begin Add(3); Add(4); Add(5); Add(6); end;
- grogs 15y agoI've missed this feature in other languages. Now I have a name, the cascades operator. I don't think the syntax is especially elegant/readable, though.
- gnaritas 15y agoIt's extremely elegant and readable, when done idiomatically. list := OrderedCollection new add: 1; add: 2; add: 3; yourself There's actually a literal syntax for Arrays, but the above example applies to all code and is used heavily in constructors and anything using a builder pattern.
- masklinn 15y ago> There's actually a literal syntax for Arrays Much in the same way as Java, smalltalk-80's arrays are not the most useful thing (they're significantly worse than Java's if I remember correctly, as you can only put other literals in them). So most collections in "user" code are not going to be arrays. I believe gnu smalltalk extends arrays to be more useful (and adds other collection literals).
- gnaritas 15y ago> as you can only put other literals in them What? That's not even close to true. You are misunderstanding something. Actually, I see I think the issue... most Smalltalks have progressed a bit since Smalltalk 80 and now have a fully functional array literal syntax whereas you're talking about the very restrictive original syntax from decades ago.
- masklinn 15y ago> Actually, I see I think the issue... most Smalltalks have progressed a bit since Smalltalk 80 and now have a fully functional array literal syntax whereas you're talking about the very restrictive original syntax from decades ago. I'm talking about Smalltalk-80, hence specifying it, but I know Dolphin Smalltalk still has that restriction.
- spullara 15y agoSo, if you know Java has a similar syntax that is the same terseness, why mention it without example? I've found that many people don't know of it: In Java this is: List<Integer> c = new ArrayList<>() {{ add(3); add(4); add(5); add(6); }};
- masklinn 15y ago> So, if you know Java has a similar syntax that is the same terseness, why mention it without example? 1. Because it's more limited in that it can only initialize an object and it requires that the type being initialized be non-final 2. Because it's kind-of a hack, using instance initializers (itself not a well-known feature) and anonymous classes (not overly common for concrete classes in java) simultanously I also like to think people can be delighted in looking for treasures after a hint, and it's not too hard to find references to that trick, whereas references to Smalltalk's cascading operators are sparse and lost in the noise.