www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - typeof(this) return wrong type

reply Andrey <andrey kabylin.ru> writes:
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
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
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.
 struct OnClickListener {
     string id;
 }

 class BaseView {
     void onCreate() {
         writeln(getSymbolsByUDA!(typeof(this), 
 OnClickListener).stringof);
     }
 }

 class MyView : BaseView {
      OnClickListener("okButton")
     void onOkButtonClick() {
         writeln("Hello world!");
     }
 }
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.
Apr 22 2017
parent reply Andrey <andrey kabylin.ru> writes:
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:
 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!");
     }
 }
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.
How to best solve this problem? I do not want using template mixin for this. It works for me using realtime.
Apr 22 2017
parent reply Basile B. <b2.temp gmx.com> writes:
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:
 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.
 struct OnClickListener {
     string id;
 }

 class BaseView {
     void onCreate() {
         writeln(getSymbolsByUDA!(typeof(this), 
 OnClickListener).stringof);
     }
 }

 class MyView : BaseView {
      OnClickListener("okButton")
     void onOkButtonClick() {
         writeln("Hello world!");
     }
 }
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.
How to best solve this problem? I do not want using template mixin for this. It works for me using realtime.
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.
Apr 22 2017
next sibling parent reply Andrey <andrey kabylin.ru> writes:
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
parent Andrey <andrey kabylin.ru> writes:
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:
 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 :(
 }
But this working well:
 this(this T)() {
     writeln(T.stringof);
 }
It suits me, thanks a lot!
Apr 22 2017
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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