On Wednesday, 3 May 2017 at 09:04:07 UTC, Jonathan M Davis wrote:
I believe that the core problem is that an alias declaration
just aliases a symbol - i.e. it just creates a new name for the
symbol. And as far as I can tell,
alias n2 = x2.n;
is actually equivalent to
alias n2 = member.n;
You get exactly the same error message if that change is made.
It's a bit like how you can call a static function with an
object rather than the struct/class(e.g. s.foo() instead of
S.foo()). Similarly, if you turn n into a member function, then
you get an error like
q.d(20): Error: this for n needs to be type member not type
outer
It's just aliasing the function, not creating a delegate or
doing a syntactic conversion. If it _were_ doing a syntactic
conversion and just making it so that everywhere you see n2, it
got changed to x.n, then I could see code like
outer o;
o.n2 = 5;
working. But that's not how alias declarations work. They just
create a new name for the symbol in the scope that they're
declared. So, the symbol isn't tied to a particular instance,
and you get the problem that you're having.
The following works with
outer2 o;
o.n2 = 5;
so it's not static, i.e. n2 is tied to the instance here.
struct outer2
{
int n;
alias n2 = n;
}
So it seems reasonable to have better semantics for an embedded
struct with alias_this.