Debian Codegraph @

gcc/testsuite/gdc.test/compilable/named_arguments.d

raw
1
2 string fun(string x, string y, string z = "Z", string w = "W")
3 {
4 return x ~ y ~ z ~ w;
5 }
6
7 static assert(fun( "x", "y") == "xyZW");
8 static assert(fun( "x", "y", "z", "w") == "xyzw");
9 static assert(fun(x: "x", y: "y", z: "z", w: "w") == "xyzw");
10 static assert(fun(w: "w", z: "z", y: "y", x: "x") == "xyzw");
11 static assert(fun(y: "y", "z", x: "x") == "xyzW");
12 static assert(fun( "x", "y", w: "w") == "xyZw");
13 static assert(fun(x: "x", "y", z: "z") == "xyzW");
14
15 // Default arguments need not all be at the end anymore
16 string fun2(string x = "x", string y, string z = "z")
17 {
18 return x ~ y ~ z;
19 }
20
21 static assert(fun2(y: "y") == "xyz");
22
23 struct SD
24 {
25 int x;
26 int y;
27
28 // The assumption that first parameter having a default implies all parameters have a default is no longer valid,
29 // so this struct constructor shouldn't be mistaken for a default constructor.
30 this(int x = 10, int y)
31 {
32 this.x = x;
33 this.y = y;
34 }
35 }
36
37 static assert(SD(y: 20) == SD(10, 20));
38
39 // UFCS
40 static assert("x".fun("y", w: "w") == "xyZw");
41
42 // tuples
43 alias AliasSeq(T...) = T;
44
45 static assert("x".fun(x: AliasSeq!(), "y", w: "w") == "xyZw");
46 static assert(AliasSeq!("x", "y").fun(w: "w", z: AliasSeq!()) == "xyZw");
47 static assert(fun(y: AliasSeq!("y", "z", "w"), x: "x") == "xyzw");
48
49 // `new` expressions
50 class C
51 {
52 int x, y;
53
54 this(int x, int y)
55 {
56 this.x = x;
57 this.y = y;
58 }
59
60 static C opCall(int x, int y) { return new C(x, y); }
61 }
62
63 struct S
64 {
65 int x, y;
66 }
67
68 static assert(new C(y: 3, x: 2).x == 2);
69 static assert( C(y: 3, x: 2).x == 2);
70 static assert(new S(y: 3, x: 2).x == 2);
71 static assert( S(y: 3, x: 2).x == 2);
72
73 // opCall
74 struct Div
75 {
76 static int opCall(int numer, int denom) { return numer / denom; }
77 }
78 static assert(Div(denom: 3, numer: 6) == 2);
79
80 struct Concat
81 {
82 string s = "s";
83 string opCall(string st0, string st1) { return s ~ st0 ~ st1; }
84 }
85
86 static assert(Concat.init("t0", "t1") == "st0t1");