Debian Codegraph @

gcc/testsuite/gdc.test/runnable/b20470.d

raw
1 // https://issues.dlang.org/show_bug.cgi?id=20470
2
3 alias AliasSeq(Args...) = Args;
4
5 int g, h;
6
7 void test0()
8 {
9 static struct S
10 {
11 int a, b;
12 float c = 0, d = 0;
13 alias fields = AliasSeq!(a, b, c, d);
14 alias ints = AliasSeq!(a, b);
15 alias floats = AliasSeq!(c, d);
16 alias reversed = AliasSeq!(d, c, b, a);
17 alias globals = AliasSeq!(g, h);
18
19 alias properties = AliasSeq!(e, f);
20 @property int e() { return a; }
21 @property void e(int i) { a = i; }
22 @property float f() { return c; }
23 @property void f(float j) { c = j; }
24 }
25
26 S s;
27 assert(s.fields == AliasSeq!(0, 0, 0, 0));
28 s.ints = AliasSeq!(1, 2);
29 assert(s.fields == AliasSeq!(1, 2, 0, 0));
30 s.floats = AliasSeq!(3, 4);
31 assert(s.fields == AliasSeq!(1, 2, 3, 4));
32
33 int a, b;
34 float c, d;
35 AliasSeq!(d, c, b, a) = s.reversed;
36 assert(AliasSeq!(a, b, c, d) == AliasSeq!(1, 2, 3, 4));
37
38 s.globals = AliasSeq!(30, 40);
39 assert(g == 30 && h == 40);
40
41 // Propagating `this` to functions and properties within tuples will be a breaking change.
42 // See `test2()` below for an example of existing code that would need to be fixed.
43
44 //s.properties = AliasSeq!(11, 12);
45 //assert(s.e == 11 && s.f == 12);
46 }
47
48 class Nested(Vars...)
49 {
50 int a, b;
51 alias outervars = Vars;
52 alias fields = AliasSeq!(a, b);
53 alias all = AliasSeq!(a, b, Vars);
54 }
55
56 auto makeNested()
57 {
58 static class C
59 {
60 bool b;
61 double d;
62 auto nested() { return new Nested!(b, d)(); }
63 }
64 return new C().nested();
65 }
66
67 void test1()
68 {
69 auto n = makeNested();
70 n.fields = AliasSeq!(1, 2);
71 n.outervars = AliasSeq!(true, 1.3);
72 assert(n.all == AliasSeq!(1, 2, true, 1.3));
73 }
74
75 void test2()
76 {
77 // backwards compatibility test for functions within tuples
78
79 static struct S
80 {
81 void f();
82 void g();
83 alias funcs = AliasSeq!(f, g);
84 }
85
86 S s;
87 alias voidTf = void();
88 foreach (f; s.funcs)
89 static assert(is(typeof(f) == voidTf));
90 }
91
92 void main()
93 {
94 test0();
95 test1();
96 test2();
97 }