digitalmars.D.learn - typeof(this) return wrong type
- Andrey (3/18) Apr 22 2017 Hello, I trying to add custom attribute OnClickListener, the
- Stefan Koch (4/24) Apr 22 2017 typeof returns a static type not a dynamic type.
- Andrey (3/27) Apr 22 2017 How to best solve this problem? I do not want using template
- Basile B. (8/36) Apr 22 2017 use a "template this" parameter:
- Andrey (2/15) Apr 22 2017
- Andrey (3/23) Apr 22 2017 It suits me, thanks a lot!
- Adam D. Ruppe (5/6) Apr 22 2017 I think the nicest way to do it is to use the template this
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/6) Apr 22 2017 There is also typeid() and .classinfo, which may be helpful:
Hello, I trying to add custom attribute OnClickListener, the problem is that typeof always return BaseView type instead of MyView.struct OnClickListener { string id; } class BaseView { void onCreate() { writeln(getSymbolsByUDA!(typeof(this), OnClickListener).stringof); } } class MyView : BaseView { OnClickListener("okButton") void onOkButtonClick() { writeln("Hello world!"); } }
Apr 22 2017
On Saturday, 22 April 2017 at 11:33:22 UTC, Andrey wrote:Hello, I trying to add custom attribute OnClickListener, the problem is that typeof always return BaseView type instead of MyView.typeof returns a static type not a dynamic type. If there is a branch of the function that does not return myview the closed base-type is used.struct OnClickListener { string id; } class BaseView { void onCreate() { writeln(getSymbolsByUDA!(typeof(this), OnClickListener).stringof); } } class MyView : BaseView { OnClickListener("okButton") void onOkButtonClick() { writeln("Hello world!"); } }
Apr 22 2017
On Saturday, 22 April 2017 at 11:36:09 UTC, Stefan Koch wrote:On Saturday, 22 April 2017 at 11:33:22 UTC, Andrey wrote:How to best solve this problem? I do not want using template mixin for this. It works for me using realtime.Hello, I trying to add custom attribute OnClickListener, the problem is that typeof always return BaseView type instead of MyView.typeof returns a static type not a dynamic type. If there is a branch of the function that does not return myview the closed base-type is used.struct OnClickListener { string id; } class BaseView { void onCreate() { writeln(getSymbolsByUDA!(typeof(this), OnClickListener).stringof); } } class MyView : BaseView { OnClickListener("okButton") void onOkButtonClick() { writeln("Hello world!"); } }
Apr 22 2017
On Saturday, 22 April 2017 at 11:45:54 UTC, Andrey wrote:On Saturday, 22 April 2017 at 11:36:09 UTC, Stefan Koch wrote:use a "template this" parameter: void onCreate(this T)() { writeln(getSymbolsByUDA!(T, OnClickListener).stringof); } if possible. The problem is not solved if onCreate is not called on the most derived.On Saturday, 22 April 2017 at 11:33:22 UTC, Andrey wrote:How to best solve this problem? I do not want using template mixin for this. It works for me using realtime.Hello, I trying to add custom attribute OnClickListener, the problem is that typeof always return BaseView type instead of MyView.typeof returns a static type not a dynamic type. If there is a branch of the function that does not return myview the closed base-type is used.struct OnClickListener { string id; } class BaseView { void onCreate() { writeln(getSymbolsByUDA!(typeof(this), OnClickListener).stringof); } } class MyView : BaseView { OnClickListener("okButton") void onOkButtonClick() { writeln("Hello world!"); } }
Apr 22 2017
On Saturday, 22 April 2017 at 12:23:32 UTC, Basile B. wrote:On Saturday, 22 April 2017 at 11:45:54 UTC, Andrey wrote: use a "template this" parameter: void onCreate(this T)() { writeln(getSymbolsByUDA!(T, OnClickListener).stringof); } if possible. The problem is not solved if onCreate is not called on the most derived.It works if I explicitly specify the type:MyView v2 = new MyView(); v2.onCreate(); // Works well, using MyView type void test(View v) { v.onCreate(); // using View type :( }
Apr 22 2017
On Saturday, 22 April 2017 at 12:43:41 UTC, Andrey wrote:On Saturday, 22 April 2017 at 12:23:32 UTC, Basile B. wrote:But this working well:On Saturday, 22 April 2017 at 11:45:54 UTC, Andrey wrote: use a "template this" parameter: void onCreate(this T)() { writeln(getSymbolsByUDA!(T, OnClickListener).stringof); } if possible. The problem is not solved if onCreate is not called on the most derived.It works if I explicitly specify the type:MyView v2 = new MyView(); v2.onCreate(); // Works well, using MyView type void test(View v) { v.onCreate(); // using View type :( }this(this T)() { writeln(T.stringof); }It suits me, thanks a lot!
Apr 22 2017
On Saturday, 22 April 2017 at 12:23:32 UTC, Basile B. wrote:use a "template this" parameter:I think the nicest way to do it is to use the template this parameter in the constructors, since that's the one function that you can guarantee is always called on the most derived type. Of course, you can still forget to call it in a child class..
Apr 22 2017
On 04/22/2017 04:33 AM, Andrey wrote:Hello, I trying to add custom attribute OnClickListener, the problem is that typeof always return BaseView type instead of MyView.There is also typeid() and .classinfo, which may be helpful: writeln(this.classinfo.name); Ali
Apr 22 2017