@@ -9,6 +9,7 @@ $(GNAME Declaration):
9
9
$(GLINK2 function, FuncDeclaration)
10
10
$(GLINK VarDeclarations)
11
11
$(GLINK AliasDeclaration)
12
+ $(GLINK AliasAssign)
12
13
$(GLINK2 struct, AggregateDeclaration)
13
14
$(GLINK2 enum, EnumDeclaration)
14
15
$(GLINK2 module, ImportDeclaration)
@@ -429,6 +430,74 @@ a = 2; // sets `S.i` to `2`
429
430
b = 4; // sets `S.j` to `4`
430
431
-----------
431
432
433
+ $(H2 $(LNAME2 AliasAssign, Alias Assign))
434
+
435
+ $(GRAMMAR
436
+ $(GNAME AliasAssign):
437
+ $(I Identifier) $(D =) $(GLINK Type)
438
+ )
439
+
440
+ $(P An $(GLINK AliasDeclaration) can have a new value assigned to it with an
441
+ $(I AliasAssign):)
442
+
443
+ ---
444
+ template Gorgon(T)
445
+ {
446
+ alias A = long;
447
+ A = T; // assign new value to A
448
+ alias Gorgon = A;
449
+ }
450
+ pragma(msg, Gorgon!int); // prints int
451
+ ---
452
+
453
+ $(UL
454
+ $(LI The $(I AliasAssign) and its corresponding $(I AliasDeclaration) must both be
455
+ declared in the same $(GLINK2 template, TemplateDeclaration).)
456
+
457
+ $(LI The corresponding $(I AliasDeclaration) must appear lexically before the
458
+ $(I AliasAssign).)
459
+
460
+ $(LI The corresponding $(I AliasDeclaration) may not refer to overloaded symbols.)
461
+
462
+ $(LI The value of an $(I AliasDeclaration) or left hand side (lvalue) of an $(I AliasAssign) may not be used prior
463
+ to another $(I AliasAssign) to the same lvalue other than in the right hand side of that $(I AliasAssign).)
464
+
465
+ )
466
+
467
+ $(BEST_PRACTICE
468
+ $(I AliasAssign) is particularly useful when using an iterative
469
+ computation rather than a recursive one, as it avoids creating
470
+ the large number of intermediate templates that the recursive one
471
+ engenders.
472
+ ---
473
+ template AliasSeq(T...) { alias AliasSeq = T; }
474
+
475
+ static if (0) // recursive method for comparison
476
+ {
477
+ template Reverse(T...)
478
+ {
479
+ static if (T.length == 0)
480
+ alias Reverse = AliasSeq!();
481
+ else
482
+ alias Reverse = AliasSeq!(Reverse!(T[1 .. T.length]), T[0]);
483
+ }
484
+ }
485
+ else // iterative method minimizes template instantiations
486
+ {
487
+ template Reverse(T...)
488
+ {
489
+ alias A = AliasSeq!();
490
+ static foreach (t; T)
491
+ A = AliasSeq!(t, A); // Alias Assign
492
+ alias Reverse = A;
493
+ }
494
+ }
495
+
496
+ enum X = 3;
497
+ alias TK = Reverse!(int, const uint, X);
498
+ pragma(msg, TK); // prints tuple(3, (const(uint)), (int))
499
+ ---
500
+ )
432
501
433
502
$(H2 $(LNAME2 alias-reassignment, Alias Reassignment))
434
503
@@ -467,6 +536,7 @@ $(H2 $(LNAME2 alias-reassignment, Alias Reassignment))
467
536
and requires significantly simpler code than the alternative recursive method.)
468
537
469
538
539
+
470
540
$(H2 $(LNAME2 extern, Extern Declarations))
471
541
472
542
$(P Variable declarations with the storage class $(D extern) are not allocated
0 commit comments