News for hackers. Stuff that matters.
Re:Strangest Language Features by masklinn on Thursday December 15, 2011 @12:59PM (#3357154)
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).

Re:Strangest Language Features by bokchoi on Thursday December 15, 2011 @04:44PM (#3358177)
> 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

And it is strange more languages don't have it.

Re:Strangest Language Features by masklinn on Friday December 16, 2011 @04:17AM (#3360107)
It 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`.
Re:Strangest Language Features by ajuc on Friday December 16, 2011 @07:46AM (#3360542)
Clojure has something even cooler than doto. It's called "thrush operators":

http://clojuredocs.org/clojure_core/clojure.core/-%3E and http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E

Re:Strangest Language Features by Someone on Thursday December 15, 2011 @04:46PM (#3358187)
'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;
Re:Strangest Language Features by spullara on Friday December 16, 2011 @12:22AM (#3359545)
So, 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); }};

Re:Strangest Language Features by masklinn on Friday December 16, 2011 @04:14AM (#3360103)
> 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.

Re:Strangest Language Features by munificent on Thursday December 15, 2011 @04:21PM (#3358062)
> 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.

Re:Strangest Language Features by grogs on Thursday December 15, 2011 @07:54PM (#3358847)
I'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.

Re:Strangest Language Features by gnaritas on Thursday December 15, 2011 @09:45PM (#3359170)
It'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.
Re:Strangest Language Features by masklinn on Friday December 16, 2011 @04:09AM (#3360089)
> 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).

Re:Strangest Language Features by gnaritas on Friday December 16, 2011 @11:17AM (#3361076)
> 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.

Re:Strangest Language Features by masklinn on Saturday December 17, 2011 @04:26AM (#3363886)
> 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.

Re:Strangest Language Features by gnaritas on Saturday December 17, 2011 @09:19PM (#3365694)
Well I brought up the Array literals in the context of modern Smalltalk; it makes little sense to respond to that by criticizing a decades old version of Smalltalk. Dolphin is a virtually dead Smalltalk, died officially a while back and was then resurrected, but it's hardly the model of a modern Smalltalk anymore. Visual Works, Pharo, Squeak, and Gemstone are modern Smalltalk's and all have a fully functional array literal syntax.