June 06, 2012 alias this with property enforce | ||||
---|---|---|---|---|
| ||||
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? |
June 07, 2012 Re: alias this with property enforce | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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.
|
Copyright © 1999-2021 by the D Language Foundation