alias this with property enforce

Kenji Hara k.hara.pg at gmail.com
Wed Jun 6 19:17:11 PDT 2012


On Wednesday, 6 June 2012 at 18:12:39 UTC, Namespace wrote:
> If i have this code:
>
> class Bar {
> public:
>   Foo GetFoo() {
>     return this._foo;
>   }
>
>   alias GetFoo this;
> }
>
> to allow access to Foo methods from Bar i get the error, that 
> "GetFoo" isn't a property if i use -property.
> Why?
> The solution is to set @property before "Foo GetFoo()" but why 
> must GetFoo a property? What if i don't want to declare GetFoo 
> as a property? I cannot use it with alias this?

Because name lookup with alias this is implemented as simple 
rewriting of expressions.

   auto bar = new Bar();
   bar.hoge;

If class Bar doesn't have member hoge, it is rewritten as:

   bar.GetFoo.hoge;

After that, if Bar.GetFoo.hoge is a property, bar.GetFoo is 
converted to bar.GetFoo().hoge as like other property functions.
If Bar.GetFoo is not a property and you use -property switch, 
compiler shows "not a property" error because GetFoo is not a 
property.

There is no magic.


More information about the Digitalmars-d-learn mailing list