Thread overview
Bug? aliasing member of instance
Oct 22, 2011
Nick Sabalausky
Oct 22, 2011
Timon Gehr
Oct 22, 2011
Nick Sabalausky
Oct 22, 2011
Timon Gehr
Oct 23, 2011
Nick Sabalausky
October 22, 2011
Is this a compiler bug?

--------------------------------
struct Foo
{
    int a;
}

Foo foo;
alias foo.a b;

void main()
{
    b = 5;  // <-- Error
}
--------------------------------

>dmd test.d
test.d(11): Error: need 'this' to access member a

I did this on DMD 2.055


October 22, 2011
On 10/22/2011 09:28 AM, Nick Sabalausky wrote:
> Is this a compiler bug?
>
> --------------------------------
> struct Foo
> {
>      int a;
> }
>
> Foo foo;
> alias foo.a b;
>
> void main()
> {
>      b = 5;  //<-- Error
> }
> --------------------------------
>
>> dmd test.d
> test.d(11): Error: need 'this' to access member a
>
> I did this on DMD 2.055
>
>
alias has never worked for instance members, even if they are accessible at compile time. The workaround for your example could be this:

ref @property b(){return foo.a;} // instead of alias foo.a b;

Another case where we currently have this meaningless error message:

struct Foo{
    int a;
    int foo(int res=a){return res;}
}

void main(){
    Foo x;
    x.foo();
}

I think that should work.


October 22, 2011
"Timon Gehr" <timon.gehr@gmx.ch> wrote in message news:j7u9ld$i8t$1@digitalmars.com...
>
> alias has never worked for instance members, even if they are accessible at compile time.

Any idea if it's supposed to work?


October 22, 2011
On 10/22/2011 11:03 PM, Nick Sabalausky wrote:
> "Timon Gehr"<timon.gehr@gmx.ch>  wrote in message
> news:j7u9ld$i8t$1@digitalmars.com...
>>
>> alias has never worked for instance members, even if they are accessible
>> at compile time.
>
> Any idea if it's supposed to work?
>
>

I do not know. But if it was not supposed to work, the error message could certainly be improved. It is odd that the alias compiles fine but cannot be used later on. That fact maybe indicates that it is supposed to work.
October 23, 2011
"Timon Gehr" <timon.gehr@gmx.ch> wrote in message news:j7ve1p$2rds$1@digitalmars.com...
> On 10/22/2011 11:03 PM, Nick Sabalausky wrote:
>> "Timon Gehr"<timon.gehr@gmx.ch>  wrote in message news:j7u9ld$i8t$1@digitalmars.com...
>>>
>>> alias has never worked for instance members, even if they are accessible at compile time.
>>
>> Any idea if it's supposed to work?
>>
>>
>
> I do not know. But if it was not supposed to work, the error message could certainly be improved. It is odd that the alias compiles fine but cannot be used later on. That fact maybe indicates that it is supposed to work.

I've filled a report. If it's invalid it can be closed or changed to "bad error message":

http://d.puremagic.com/issues/show_bug.cgi?id=6842


October 24, 2011
On Sat, 22 Oct 2011 03:28:53 -0400, Nick Sabalausky <a@a.a> wrote:

> Is this a compiler bug?
>
> --------------------------------
> struct Foo
> {
>     int a;
> }
>
> Foo foo;
> alias foo.a b;
>
> void main()
> {
>     b = 5;  // <-- Error
> }
> --------------------------------
>
>> dmd test.d
> test.d(11): Error: need 'this' to access member a
>
> I did this on DMD 2.055

alias works on *symbols* not data.

For example, if Foo has a function bar(), aliasing foo.bar is equivalent to aliasing Foo.bar.  You are aliasing the *function* not the *delegate*.

So naturally, it would make sense that aliasing foo.a is equivalent to aliasing Foo.a, or the symbol that accesses the member 'a' in an instance of 'Foo'.

I would expect this may work:

void main()
{
   foo.b = 5;
}

-Steve