alias can't find symbol or can't use symbol

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 6 08:13:19 PDT 2017


On Saturday, May 6, 2017 1:49:34 PM CEST Carl Sturtivant via Digitalmars-d-
learn wrote:
> 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.

But that's fundamentally different from what you were trying to do before.
With

struct outer2
{
    int n;
    alias n2 = n;
}

auter2 o;
o.n2 = 5;

the alias is an alias to something in the same scope which does not require
a different this pointer/reference. No additional information is required.
On the other hand, with

struct A
{
    int n;
}

struct B
{
    B x;
    alias n2 = x.n;
}

A a;
a.n2 = 5;

it needs to use the member variable x to get at n. So, for that to work, it
needs to do more than simply alias the symbol, since a symbol is not tied to
an instance.

I agree that it would be nice if both examples worked, but that isn't how
alias declarations currently work. I don't know how hard it would be to talk
Walter into changing it. It may be that he would be easy to convince, or it
could be that he'd be against it. I don't know.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list