It looks like it is the same as clojure's doto macro:
http://clojuredocs.org/clojure_core/clojure.core/doto
And it is strange more languages don't have it.
http://clojuredocs.org/clojure_core/clojure.core/-%3E and http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E
c := LeftAsAnExerciseForTheReader();
with c do
begin
Add(3);
Add(4);
Add(5);
Add(6);
end;In Java this is:
List<Integer> c = new ArrayList<>() {{ add(3); add(4); add(5); add(6); }};
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.
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.
Now I have a name, the cascades operator.
I don't think the syntax is especially elegant/readable, though.
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.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).
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.
I'm talking about Smalltalk-80, hence specifying it, but I know Dolphin Smalltalk still has that restriction.
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:
(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:
is equivalent to: (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).