Skip to content

Commit 115e239

Browse files
authored
Merge pull request #2868 from WalterBright/AliasAssign
add new feature AliasAssign to spec
2 parents 969ceb1 + 5831167 commit 115e239

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

spec/declaration.dd

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ $(GNAME Declaration):
99
$(GLINK2 function, FuncDeclaration)
1010
$(GLINK VarDeclarations)
1111
$(GLINK AliasDeclaration)
12+
$(GLINK AliasAssign)
1213
$(GLINK2 struct, AggregateDeclaration)
1314
$(GLINK2 enum, EnumDeclaration)
1415
$(GLINK2 module, ImportDeclaration)
@@ -429,6 +430,74 @@ a = 2; // sets `S.i` to `2`
429430
b = 4; // sets `S.j` to `4`
430431
-----------
431432

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+
)
432501

433502
$(H2 $(LNAME2 alias-reassignment, Alias Reassignment))
434503

@@ -467,6 +536,7 @@ $(H2 $(LNAME2 alias-reassignment, Alias Reassignment))
467536
and requires significantly simpler code than the alternative recursive method.)
468537

469538

539+
470540
$(H2 $(LNAME2 extern, Extern Declarations))
471541

472542
$(P Variable declarations with the storage class $(D extern) are not allocated

0 commit comments

Comments
 (0)