Skip to content

Commit 969ceb1

Browse files
authored
Merge pull request #2919 from WalterBright/AliasReassignment
spec: add AliasReassignment
2 parents c72d652 + bdd2d2b commit 969ceb1

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

spec/declaration.dd

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,44 @@ a = 2; // sets `S.i` to `2`
429429
b = 4; // sets `S.j` to `4`
430430
-----------
431431

432+
433+
$(H2 $(LNAME2 alias-reassignment, Alias Reassignment))
434+
435+
$(GRAMMAR
436+
$(GNAME AliasReassignment):
437+
$(GLINK_LEX Identifier) $(D =) $(GLINK StorageClasses)$(OPT) $(GLINK Type)
438+
$(GLINK_LEX Identifier) $(D =) $(GLINK2 expression, FunctionLiteral)
439+
$(GLINK_LEX Identifier) $(D =) $(GLINK StorageClasses)$(OPT) $(GLINK BasicType) $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT)
440+
)
441+
442+
$(P An alias declaration inside a template can be reassigned a new value.)
443+
444+
---
445+
template staticMap(alias F, T...)
446+
{
447+
alias A = AliasSeq!();
448+
static foreach (t; T)
449+
A = AliasSeq!(A, F!T); // alias reassignment
450+
alias staticMap = A;
451+
}
452+
---
453+
454+
$(P The $(I Identifier) must resolve to a lexically preceding $(GLINK AliasDeclaration).
455+
Both must be members of the same $(GLINK2 template, TemplateDeclaration).
456+
)
457+
458+
$(P The right hand side of the $(I AliasReassignment) replaces the right hand side of the
459+
$(I AliasDeclaration).
460+
)
461+
462+
$(P Once the $(I AliasDeclaration) has been referred to in any context other than the
463+
right hand side of an $(I AliasReassignment) it can no longer be reassigned.
464+
)
465+
466+
$(RATIONALE Alias reassignment can result in faster compile times and lowered memory consumption,
467+
and requires significantly simpler code than the alternative recursive method.)
468+
469+
432470
$(H2 $(LNAME2 extern, Extern Declarations))
433471

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

0 commit comments

Comments
 (0)