3 ms·
No, the alternative is this: class Point(shared Float x, shared Float y) { string => "(``x``, ``y``)"; } I think I made that really clear in t
by gavinking 11y ago
No, the alternative is this:
class Point(shared Float x, shared Float y) {
string => "(``x``, ``y``)";
}
I think I made that really clear in the post. In the very example I started with, but perhaps I need to further clarify it somehow?
UPDATE: FTR, I added the above example to my post, in the hope that it clarifies the point. Thanks for the feedback.
- couchand 11y agoThat seems to work until you want to add another constructor, or am I misreading the post?
- gavinking 11y agoRight, exactly. So there's two very distinct cases here: - the simple and overwhelmingly common case where there is only one way to instantiate a class, and therefore there is no reason to decouple the body of the class from the parameter list, and - the occasional but also important case where there are multiple ways to instantiate the class, each with different parameter list signatures, where we need to decouple the body of the class from the parameter list signatures. "Constructors" as supported in Java, C++, C#, and now also in Ceylon, are optimized for the "occasional" case. And I'm convinced that they're useful enough that we need them. But in the "overwhelmingly common" case they get in the way, and that's why they're not the usual thing you reach for first when writing a class in Ceylon.
- mborch 11y agoDart uses the following sugar for a "simple" constructor: Color(this.rgba); That is, it directly assigns the argument to the slot (which must be defined at class-level.) Of course, it's still repeating the class name but that's solvable.
- mikegioia 11y agoWhy do all of your examples after the first violate the advantage you reference in the first example?
- gavinking 11y agoBecause the post is dealing with the new functionality we've added in Ceylon 1.2, which provides support for Java-style (i.e. multiple) constructors, not the functionality that has always existed in Ceylon, where parameters of the class are listed at the top of the class declaration. Does that make sense now?
- AndrewDucker 11y agoThank you - that's clear. Seems similar to C# with its "Primary Constructor": http://odetocode.com/blogs/scott/archive/2014/08/14/c-6-0-features-part-ii-primary-constructors.aspx http://odetocode.com/blogs/scott/archive/2014/08/14/c-6-0-fe...