#1777 Method cascades

KevinKelley Sat 18 Feb 2012

Method cascades proposed for Dart...

Here, ".." is the cascaded method invocation operation. The ".." syntax invokes a method (or setter or getter) but discards the result, and returns the original receiver instead.

I really like that, and if it weren't for the range operator which I also like, I'd suggest this for immediate drop-in to Fantom.

Fantom's got it-block constructors, and comma-operator-means-add, so a couple of use cases for .. are covered already. But I do wish we had this, for method chaining. Current practice of returning This is marginally helpful -- too often it interferes with the "normal" signature of a method, and you have to plan for or around it.

Too many obscure operators is a problem, but all the good ones are taken! I think we should move programming toward Earley/Tomita -style bottom-up parsing, and let meaning be determined by surrounding context...

ikhwanhayat Sat 18 Feb 2012

Fantom can already do this:

obj.with {
  foo()
  bar()
}

Compared to (proposed) Dart's

obj
  ..foo()
  ..bar()

Doesn't seem so much different. Fantom's with is almost the same as VB's.

With obj
  .Foo()
  .Bar()
End With

One of the thing I miss when migrating from VB to C# :)

ikhwanhayat Sat 18 Feb 2012

Oops, you can actually do this in Fantom. Even better :)

obj {
  foo()
  bar()
}

KevinKelley Sat 18 Feb 2012

Hmm, of course, you're right.

qualidafial Sat 18 Feb 2012

@ikhwanhayat doesn't C# have a using keyword that's similar to with?

helium Sat 18 Feb 2012

No, using is something completely different. It's like Java's new try-with-resources.

using (var stream = new StreamReader("...")) {
   readSomethingFromStream(stream);
} // stream gets automatically closed here

ikhwanhayat Mon 20 Feb 2012

Yeah, at best what you can do in C# is to shorten the variable name:

using (var o = obj) {
  o.Foo();
  o.Bar();
}

Login or Signup to reply.