2

Smalltalk supports a syntax feature called "message cascades". Cascades are being adopted by the Dart Programming language.

As far as I know, C# doesn't support this. Were they ever considered during the design of the language? Is it conceivable that they could appear in a future version of the language?

2
  • look at LinQ, is that what u look for?
    – Yusubov
    Commented Jul 15, 2012 at 2:47
  • 2
    Notice also that cascade methods are very useful in Smalltalk because if there is no explicit return in a method, a method answers 'self', therefore, cascading is easier.
    – user59354
    Commented Jul 15, 2012 at 20:22

1 Answer 1

16

Fluent interfaces are easily supported in C# by using return this.

In Linq, extension methods are used to accomplish the same thing. Extension methods over IQueryable or IEnumerable are easily chained together.

Cascades, as described by the page at the link you provided, seem to be merely a subtle refinement of an ordinary Fluent Interface where return this is not required to make it work, as all method calls beginning with .. refer to the first returned result:

query('#my-form').query('button')
    ..classes.add('toggle')
    ..text = 'Click Me!'
    ..labels.add(toggleLabel);

Does C# support this? No, not exactly. But you can still write the following equivalent code:

var result = query('#my-form').query('button')
result.classes.add('toggle')
result.text = 'Click Me!'
result.labels.add(toggleLabel);

and it is only slightly more verbose (and arguably easier to read).

As to the question, "Were they ever considered for C#," you would have to ask Anders Hejlsberg or Eric Lippert about that. My guess is that they view it as syntactic sugar, which in fact it is.

1
  • 3
    The point is that you can re-use the methods in other contexts where you might need the return value. This makes sense when getters and setters are in fact the same method (i.e. a setter returns the old value and sets the argument, if given). This of course doesn’t fit idiomatic C# code anyway. Commented Jul 16, 2012 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.