Skip to content

Commit d615a92

Browse files
ntreldlang-bot
authored andcommitted
[spec/declaration] Document field aliases
1 parent 5a3a699 commit d615a92

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

spec/declaration.dd

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -411,23 +411,49 @@ void main()
411411

412412
$(H3 $(LNAME2 alias-variable, Aliasing Variables))
413413

414-
$(P Aliases cannot be used for expressions:)
414+
$(P Variables can be aliased, expressions cannot:)
415+
416+
$(SPEC_RUNNABLE_EXAMPLE_RUN
417+
-----------
418+
int i = 0;
419+
alias a = i; // OK
420+
alias b = a; // alias a variable alias
421+
a++;
422+
b++;
423+
assert(i == 2);
424+
425+
//alias c = i * 2; // error
426+
//alias d = i + i; // error
427+
-----------
428+
)
429+
430+
$(P Members of an aggregate can be aliased, however non-static
431+
field aliases cannot be accessed outside their parent type.)
415432

416433
$(SPEC_RUNNABLE_EXAMPLE_RUN
417434
-----------
418435
struct S
419436
{
420-
static int i;
421-
static int j;
437+
static int i = 0;
438+
int j;
439+
alias a = j; // OK
440+
441+
void inc() { a++; }
422442
}
423443

424-
alias a = S.i; // OK, `S.i` is a symbol
425-
alias b = S.j; // OK. `S.j` is also a symbol
426-
//alias c = a + b; // illegal, `a + b` is an expression
427-
a = 2; // sets `S.i` to `2`
428-
b = 4; // sets `S.j` to `4`
429-
assert(S.i == 2);
430-
assert(S.j == 4);
444+
alias a = S.i; // OK
445+
a++;
446+
assert(S.i == 1);
447+
448+
alias b = S.j; // allowed
449+
static assert(b.offsetof == 0);
450+
//b++; // error, no instance of S
451+
//S.a++; // error, no instance of S
452+
453+
S s = S(5);
454+
s.inc();
455+
assert(s.j == 6);
456+
//alias c = s.j; // scheduled for deprecation
431457
-----------
432458
)
433459

0 commit comments

Comments
 (0)