4 ms·
As alluded above, the biggest problem with the constructor syntax in languages that borrow from C++ is that in the common case of a class with just one construc
by couchand 11y ago
As alluded above, the biggest problem with the constructor syntax in languages that borrow from C++ is that in the common case of a class with just one constructor, the parameters of that constructor aren't available in the body of the class, leading to awful code like the following:
class Point {
public float x;
public float y;
public Point(float x, float y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
This hurts. Fortunately, we've already made that pain go away in Ceylon.
Well that seems like a laudable goal. Let's take a look at the example code, then, to see what the alternative is:
class Color {
shared Integer rgba;
//default constructor
shared new (Integer rgba) {
assert (0 <= rgba <= #FFFFFFFF);
this.rgba = rgba;
}
...
string => "Color { \
alpha=``hex(alpha)``, \
red=``hex(red)``, \
green=``hex(green)``, \
blue=``hex(blue)`` }";
}
Maybe it's just me but it looks like the pain is still there?
- gavinking 11y agoNo, 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...