A better way to deal with overloading?
Alexandru Ermicioi via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jan 27 03:16:18 PST 2017
On Thursday, 26 January 2017 at 00:02:03 UTC, Profile Anaysis
wrote:
> auto t1 = T1(a,b,new X(c1,c2,c3));
> auto t2 = T2(e);
> auto t3 = T3(f,g,c);
>
> and then f(t1,t2,t3);
>
> or other wise simply inline the above.
>
>
> This also cuts down on constructor overloading.
>
> This is sort of liked named parameters but the idea is that the
> compiler simply constructs the type internally as it knows what
> type to expect and the grouping symbols allow one to specify
> the contents unambiguously.
You can init structs, and classes inside the function call. Ex:
import std.stdio;
struct T1 {
int a;
int b;
int c;
}
struct T2 {
int b;
string a;
T1 t;
}
class T3 {
int z;
int m;
this(int z, int m) {
this.z = z;
this.m = m;
}
}
void foo(T1, T2, T3) {
}
void main() {
foo(
T1(1, 2, 3), // arguments are passed as rvalues to func.
T2(2, "tested",T1(1, 2, 3)), // compound struct
new T3(10, 20)
);
}
If new is not desired to be in your code, it's possible to use
opCall overload to mimic structs initialization, for classes.
Alexandru.
More information about the Digitalmars-d
mailing list