-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
mixin1.d
1470 lines (1184 loc) · 21 KB
/
mixin1.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module mixin1;
import std.stdio;
alias TypeTuple(T...) = T;
/*******************************************/
mixin template Foo(T)
{
T x;
}
mixin Foo!(uint);
struct Bar
{
template Abc(T)
{
T y;
}
template Def(T)
{
T z;
}
}
mixin Bar.Abc!(int);
Bar b;
mixin typeof(b).Def!(int);
void test1()
{
x = 3;
assert(x == 3);
y = 4;
assert(y == 4);
z = 5;
assert(z == 5);
}
/*******************************************/
template Foo2(T)
{
T x2 = T.sizeof;
}
mixin Foo2!(uint) B2;
mixin Foo2!(long) C2;
mixin Foo2!(int);
void test2()
{
B2.x2 = 3;
assert(B2.x2 == 3);
assert(C2.x2 == long.sizeof);
// assert(x2 == int.sizeof);
}
/*******************************************/
template Foo3(T)
{
int func() { printf("Foo3.func()\n"); return 1; }
}
class Bar3
{
mixin Foo3!(int);
}
class Code3 : Bar3
{
override int func() { printf("Code3.func()\n"); return 2; }
}
void test3()
{
int i;
Bar3 b = new Bar3();
i = b.func();
assert(i == 1);
b = new Code3();
i = b.func();
assert(i == 2);
}
/*******************************************/
template Foo4(T)
{
int func() { printf("Foo4.func()\n"); return 1; }
}
struct Bar4
{
mixin Foo4!(int);
}
void test4()
{
int i;
Bar4 b;
i = b.func();
assert(i == 1);
}
/*******************************************/
template Foo5()
{
int func() { printf("Foo5.func()\n"); return 1; }
}
struct Bar5
{
mixin Foo5;
}
void test5()
{
int i;
Bar5 b;
i = b.func();
assert(i == 1);
}
/*******************************************/
template Foo6()
{
int x = 5;
}
struct Bar6
{
mixin Foo6;
}
void test6()
{
int i;
Bar6 b;
i = b.x;
assert(i == 5);
assert(b.sizeof == int.sizeof);
}
/*******************************************/
template Foo7()
{
int x = 5;
}
class Bar7
{
int y = 6;
mixin Foo7;
}
void test7()
{
int i;
Bar7 b = new Bar7();
i = b.x;
printf("b.x = %d\n", b.x);
assert(i == 5);
}
/*******************************************/
template Foo8()
{
int x = 5;
int bar() { return 7; }
}
void test8()
{
mixin Foo8;
printf("x = %d\n", x);
assert(x == 5);
assert(bar() == 7);
}
/*******************************************/
template Foo9()
{
int abc() { return y; }
}
void test9()
{
int y = 8;
mixin Foo9;
assert(abc() == 8);
}
/*******************************************/
template Foo10(alias b)
{
typeof(b) abc() { return b; }
}
void test10()
{
int y = 8;
mixin Foo10!(y);
assert(abc() == 8);
}
/*******************************************/
template Foo11(alias b)
{
int abc() { return b; }
}
void test11()
{
int y = 8;
mixin Foo11!(y) B;
assert(B.abc() == 8);
}
/*******************************************/
template duff_for(alias id1, alias id2, alias s)
{
void duff_for()
{
printf("duff_for(%d, %d)\n", id1, id2);
typeof(id1) id = id1;
printf("fid = %d, %d\n", id, (id2 - id) % 8);
switch ((id2 - id) % 8)
{
case 0:
while (id != id2)
{
printf("wid = %d\n", id);
s(); ++id;
goto case;
case 7: s(); ++id; goto case;
case 6: s(); ++id; goto case;
case 5: s(); ++id; goto case;
case 4: s(); ++id; goto case;
case 3: s(); ++id; goto case;
case 2: s(); ++id; goto case;
case 1: s(); ++id;
break;
default: assert(0);
}
}
}
}
void foo12() { printf("foo12\n"); }
void test12()
{
int i = 1;
int j = 11;
mixin duff_for!(i, j, delegate void() { foo12(); });
duff_for();
}
/*******************************************/
template duff(alias id1, alias id2, alias s)
{
void duff()
{
s();
s();
}
}
void foo13(int j)
{
printf("foo13 j = %d\n", j);
assert(j == 1);
}
void test13()
{
int i = 1;
int j = 11;
mixin duff!(i, j, delegate { foo13(i); });
duff();
}
/*******************************************/
template Foo14()
{
int x14 = 5;
}
void test14()
{
int x14 = 6;
mixin Foo14;
printf("x14 = %d\n", x14);
assert(x14 == 6);
}
/*******************************************/
template Foo15()
{
int x15 = 5;
int bar15() { return x15; }
}
int x15 = 6;
mixin Foo15;
void test15()
{
printf("x15 = %d\n", x15);
printf("bar15() = %d\n", bar15());
assert(x15 == 6);
assert(bar15() == 5);
}
/*******************************************/
template Foo16()
{
int x16 = 5;
int bar() { return x16; }
}
mixin Foo16 A16;
int x16 = 6;
mixin Foo16 B16;
void test16()
{
printf("x16 = %d\n", x16);
printf("bar() = %d\n", A16.bar());
assert(x16 == 6);
assert(A16.x16 == 5);
assert(B16.x16 == 5);
assert(A16.bar() == 5);
assert(B16.bar() == 5);
}
/*******************************************/
template Foo17()
{
int x17 = 5;
}
mixin Foo17;
struct Bar17
{
mixin Foo17;
}
void test17()
{
printf("x17 = %d\n", x17); // prints 5
assert(x17 == 5);
{ Bar17 b;
int x17 = 3;
printf("b.x17 = %d\n", b.x17); // prints 5
assert(b.x17 == 5);
printf("x17 = %d\n", x17); // prints 3
assert(x17 == 3);
{
mixin Foo17;
printf("x17 = %d\n", x17); // prints 5
assert(x17 == 5);
x17 = 4;
printf("x17 = %d\n", x17); // prints 4
assert(x17 == 4);
}
printf("x17 = %d\n", x17); // prints 3
assert(x17 == 3);
}
printf("x17 = %d\n", x17); // prints 5
assert(x17 == 5);
}
/*******************************************/
template Foo18() { int z = 3; }
struct Bar18(alias Tmpl)
{
mixin Tmpl;
}
Bar18!(Foo18) b18;
void test18()
{
assert(b18.z == 3);
}
/*******************************************/
template Mix1(T)
{
int foo19(T a) { return 2*a; }
}
template Mix2(T)
{
mixin Mix1!(T);
int bar19(T a) { return foo19(a); }
}
mixin Mix2!(int);
void test19()
{
int i;
i = bar19(7);
assert(i == 14);
}
/*******************************************/
interface A20 { int f(); }
template Foo20()
{
int f()
{
printf("in C20.f()\n");
return 6;
}
}
class C20 : A20
{
mixin Foo20;
// void f() { printf("in C20.f()\n"); }
}
void test20()
{
C20 c = new C20();
int i = c.f();
assert(i == 6);
}
/*******************************************/
template Mix21() { this(int x) { printf("mix1\n"); }}
class Bar21
{
int myx;
mixin Mix21; // wouldn't compile
this() { myx = 15; }
// mixin Mix21; // placing it here compiles
}
void test21()
{
Bar21 bar = new Bar21();
}
/*******************************************/
template A22(T)
{
this()
{ int i;
i = super.foo();
assert(i == 67);
}
}
class B22
{
int foo() { printf("B22.foo()\n"); return 67; }
}
class C22 : B22
{
mixin A22!(C22);
}
void test22()
{
C22 c = new C22;
}
/*******************************************/
template Foo23()
{
const int x = 5;
}
class C23
{
mixin Foo23 F;
}
struct D23
{
mixin Foo23 F;
}
void test23()
{
C23 c = new C23;
printf("%d\n",c.F.x);
assert(c.F.x == 5);
D23 d;
printf("%d\n",d.F.x);
assert(d.F.x == 5);
}
/*******************************************/
template T24()
{
void foo24() { return cast(void)0; }
// alias foo24 foo24;
}
mixin T24;
void test24()
{
foo24();
}
/*******************************************/
template ctor25()
{
this() { this(null); }
this( Object o ) {}
}
class Foo25
{
mixin ctor25;
}
void test25()
{
Foo25 foo = new Foo25();
}
/*******************************************/
template Get26(T)
{
Reader get (ref T x)
{
return this;
}
}
class Reader
{
mixin Get26!(byte) bar;
alias bar.get get;
mixin Get26!(int) beq;
alias beq.get get;
}
void test26()
{
Reader r = new Reader;
Reader s;
byte q;
s = r.get (q);
assert(s == r);
}
/*******************************************/
template Template(int L)
{
int i = L;
int foo(int b = Template!(9).i) {
return b;
}
}
void test27()
{
int i = 10;
int foo(int b = Template!(9).i) {
return b;
}
assert(foo()==9);
}
/*******************************************/
template Blah28(int a, alias B)
{
mixin Blah28!(a-1, B);
//mixin Blah28!(0, B);
}
template Blah28(int a:0, alias B)
{
}
void test28()
{
int a;
mixin Blah28!(5,a);
printf("a = %d\n", a);
}
/*******************************************/
template T29()
{
int x;
}
struct S29
{
mixin T29;
int y;
}
const S29 s29 = { x:2, y:3 };
void test29()
{
assert(s29.x == 2);
assert(s29.y == 3);
}
/*******************************************/
class A30
{
template ctor(Type)
{
this(Type[] arr)
{
foreach(Type v; arr) writeln(typeid(typeof(v)));
}
}
mixin ctor!(int);
}
void test30()
{
static int[] ints = [0,1,2,3];
A30 a = new A30(ints);
}
/*******************************************/
template Share(T) {
const bool opEquals(ref const T x) { return true; }
}
struct List31(T) {
//int opEquals(List31 x) { return 0; }
mixin Share!(List31);
}
void test31()
{
List31!(int) x;
List31!(int) y;
int i = x == y;
assert(i == 1);
}
/*******************************************/
template Blah(int a, alias B)
{
mixin Blah!(a-1, B);
}
template Blah(int a:0, alias B)
{
int foo()
{ return B + 1;
}
}
void test32()
{
int a = 3;
mixin Blah!(5,a);
assert(foo() == 4);
}
/*******************************************/
template T33( int i )
{
int foo()
{
printf("foo %d\n", i );
return i;
}
int opCall()
{
printf("opCall %d\n", i );
return i;
}
}
class C33
{
mixin T33!( 1 ) t1;
mixin T33!( 2 ) t2;
}
void test33()
{
int i;
C33 c1 = new C33;
i = c1.t1.foo();
assert(i == 1);
i = c1.t2.foo();
assert(i == 2);
i = c1.t1();
assert(i == 1);
i = c1.t2();
assert(i == 2);
}
/*******************************************/
template mix34()
{
int i;
void print()
{
printf( "%d %d\n", i, j );
assert(i == 0);
assert(j == 0);
}
}
void test34()
{
int j;
mixin mix34!();
print();
//printf( "%i\n", i );
}
/*******************************************/
mixin T35!(int) m35;
template T35(t)
{
t a;
}
void test35()
{
m35.a = 3;
}
/*******************************************/
struct Foo36
{
int a;
mixin T!(int) m;
template T(t)
{
t b;
}
int c;
}
void test36()
{
Foo36 f;
printf("f.sizeof = %d\n", f.sizeof);
assert(f.sizeof == 12);
f.a = 1;
f.m.b = 2;
f.c = 3;
assert(f.a == 1);
assert(f.m.b == 2);
assert(f.c == 3);
}
/*******************************************/
template Foo37()
{
template func() {
int func() {
return 6;
}
}
}
class Baz37
{
mixin Foo37 bar;
}
void test37()
{
Baz37 b = new Baz37;
auto i = b.bar.func!()();
assert(i == 6);
i = (new Baz37).bar.func!()();
assert(i == 6);
}
/*******************************************/
template Foo38()
{
int a = 4;
~this()
{
printf("one\n");
assert(a == 4);
assert(b == 5);
c++;
}
}
class Outer38
{ int b = 5;
static int c;
mixin Foo38!() bar;
mixin Foo38!() abc;
~this()
{
printf("two\n");
assert(b == 5);
assert(c == 0);
c++;
}
}
void test38()
{
Outer38 o = new Outer38();
delete o;
assert(Outer38.c == 3);
}
/*******************************************/
template TDtor()
{
~this()
{
printf("Mixed-in dtor\n");
}
}
class Base39
{
~this()
{
printf("Base39 dtor\n");
}
}
class Class39 : Base39
{
mixin TDtor A;
mixin TDtor B;
~this()
{
printf("Class39 dtor\n");
}
}
void test39()
{
auto test = new Class39;
}
/*******************************************/
template Mix40()
{
int i;
}
struct Z40
{
union { mixin Mix40; }
}
void test40()
{
Z40 z;
z.i = 3;
}
/*******************************************/
class X41(P...)
{
alias P[0] Q;
mixin Q!();
}
template MYP()
{
void foo() { }
}
void test41()
{
X41!(MYP) x;
}
/*******************************************/
// 2245
template TCALL2245a(ARGS...)
{
int makecall(ARGS args)
{
return args.length;
}
}
template TCALL2245b(int n)
{
int makecall2(ARGS...)(ARGS args) if (ARGS.length == n)
{
return args.length;
}
}
class C2245
{
mixin TCALL2245a!();
mixin TCALL2245a!(int);
mixin TCALL2245a!(int,int);