www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How close can one get to overload parameter names only?

reply "deed" <none none.none> writes:
Simple example:

struct Circle {
     double radius;
     this(double radius)   { this.radius = radius;       }
     this(double diameter) { this.radius = diameter / 2; }
}

void main() {
     auto c1 = Circle(radius = 1.0);
     auto c2 = Circle(diameter = 2.0);
     assert(c1.radius == c2.radius);
}

Is it possible to get this kind of convenience?
Any planned support for it?
Oct 04 2013
parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, October 04, 2013 22:40:35 deed wrote:
 Simple example:
 
 struct Circle {
 double radius;
 this(double radius) { this.radius = radius; }
 this(double diameter) { this.radius = diameter / 2; }
 }
 
 void main() {
 auto c1 = Circle(radius = 1.0);
 auto c2 = Circle(diameter = 2.0);
 assert(c1.radius == c2.radius);
 }
 
 Is it possible to get this kind of convenience?
 Any planned support for it?
No. The name of function parameters are irrelevant outside of the function itself. If you prototype the function, then you don't even have to give parameter names. e.g. void foo(double); D uses the C linking model just like C++ does, and with that, the type of a function is based entirely on the types of its parameters and its return type. It will never be the case that D will support overloading functions using the names of parameters. It _might_ be the case that named function arguments would be added at some point (which would allow for the reordering of function arguments), but there are no plans for that at this point, and even if they were added, overloads of a function would still have to differ by their parameter types. It just doesn't work with the C linking model to do otherwise. - Jonathan M Davis
Oct 04 2013