digitalmars.D.learn - alias this with property enforce
- Namespace (14/14) Jun 06 2012 If i have this code:
- Kenji Hara (13/27) Jun 06 2012 Because name lookup with alias this is implemented as simple
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?
Jun 06 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.
Jun 06 2012