typeof(this) return wrong type

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 22 05:23:32 PDT 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.



More information about the Digitalmars-d-learn mailing list