Skip to content

Commit 9e5bca2

Browse files
authored
[spec/legacy] Add entry for aliasing an instance member (#3831)
See dlang/dmd#15863.
1 parent ea2c81f commit 9e5bca2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

spec/legacy.dd

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ $(COMMENT
2020
$(TROW `alias` target first syntax, use `alias name = target` instead.)
2121
$(TROW Struct/union postblit, use a $(DDSUBLINK spec/struct, struct-copy-constructor,
2222
copy constructor) instead.)
23+
$(TROW $(RELATIVE_LINK2 alias-instance-member, Aliasing an instance member),
24+
use `typeof(instance).member` instead.)
2325
)
2426

2527
$(H2 $(LNAME2 body, `body` keyword))
@@ -54,6 +56,36 @@ $(H3 Corrective Action)
5456
(e.g. javascript) or auto-generating code.
5557
)
5658

59+
$(H2 $(LNAME2 alias-instance-member, Aliasing an instance member))
60+
61+
$(P E.g. `alias a = instance.field;`.
62+
Such an alias actually aliases a member of the instance's *type*, not
63+
the instance member itself. That could be confusing.
64+
Instead, alias a member of the type.)
65+
66+
$(NOTE Aliasing `this.member` and `super.member` are valid inside
67+
methods.)
68+
69+
---
70+
struct Bar
71+
{
72+
Foo f;
73+
alias v = f.v; // Error, use `typeof(f).v`
74+
}
75+
76+
struct Foo
77+
{
78+
int v;
79+
void test(Foo that) const
80+
{
81+
alias a = this.v; // OK
82+
alias b = that.v; // Error, use `typeof(that).v` instead
83+
assert(&a is &b); // passes
84+
assert(&b !is &that.v);
85+
}
86+
}
87+
---
88+
5789

5890
$(SPEC_SUBNAV_PREV glossary, Glossary)
5991
)

0 commit comments

Comments
 (0)