Fix Issue 18104: improve example showing that aliases cannot be used …#2457
Fix Issue 18104: improve example showing that aliases cannot be used …#2457dlang-bot merged 2 commits intodlang:masterfrom ghost91-:fix-issue-18104
Conversation
|
Thanks for your pull request and interest in making D better, @ghost91-! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
|
spec/declaration.dd
Outdated
| alias a = s.i; // illegal, s.i is an expression | ||
| alias b = S.i; // ok | ||
| alias a = s.i; // ok, s.i is a symbol | ||
| alias b = S.i; // ok, S.i also is a symbol |
There was a problem hiding this comment.
alias b = s.i seems redundant and doesn't illustrate anything new. I suggest maybe adding a member static int j; to S and use that instead to illustrate the idea, or something like that.
There was a problem hiding this comment.
You mean something like this?
struct S
{
int i;
static int j;
}
S s;
alias a = s.i; // ok, s.i is a symbol
alias b = S.j; // ok, S.j also is a symbol
alias c = a + b; // illegal, a + b is an expression
a = 2; // sets s.i to 2
b = 4; // sets S.j to 4There was a problem hiding this comment.
a = 2; // sets s.i to 2
Doesn't work like that. An alias of s.i is the same as one of S.i. It doesn't carry this with it. It can't be used to access s.i.
There was a problem hiding this comment.
Oh yeah, of course 🤦♂️ I suppose I was bit too quick here. Do you think, we should just omit the non static case in the example, or show that accessing in the non static case is not actually possible:
struct S
{
int i;
static int j;
}
S s;
alias a = s.i; // ok, s.i is a symbol
alias b = S.j; // ok, S.j also is a symbol
alias c = a + b; // illegal, a + b is an expression
a = 2; // error, a is aliased to S.i, but does not carry a this pointer with it
b = 4; // sets S.j to 4There was a problem hiding this comment.
Or should we simplify the example even more? After all, it is about alias not working on expressions. Structs are not necessary to show this:
int a;
alias b = a; // ok, a is a symbol
alias c = a + a; // illegal, a + a is an expression|
I recommend this: struct S
{
static int i;
static int j;
}
alias a = S.i; // OK, `S.i` is a symbol
alias b = S.j; // OK. `S.j` is also a symbol
alias c = a + b; // illegal, `a + b` is an expression
a = 2; // sets `S.i` to `2`
b = 4; // sets `S.j` to `4`This purpose of this example is simply to illustrate the one can't alias an expression. Trying to show that |
|
Sounds good to me, I will change things accordingly this evening |
…for expressions