Thread overview
typeof(this) return wrong type
Apr 22, 2017
Andrey
Apr 22, 2017
Stefan Koch
Apr 22, 2017
Andrey
Apr 22, 2017
Basile B.
Apr 22, 2017
Andrey
Apr 22, 2017
Andrey
Apr 22, 2017
Adam D. Ruppe
Apr 22, 2017
Ali Çehreli
April 22, 2017
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!");
>     }
> }
April 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.
>> 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.
April 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:
>> 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.
April 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:
>> 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.

April 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 :(
> }
April 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:
>> 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!
April 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..

April 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