Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
July 26, 2017 Why can't typeof() be used in member method? | ||||
---|---|---|---|---|
| ||||
Hi, I try to track down why some complex logic is not working. I think the root issue is that typeof() is not working in member methods. I reduced it to following example: app.d(16): Error: this for Left needs to be type TBounds not type app.A Failed: ["dmd", "-v", "-o-", "app.d", "-I."] class TBounds { @property float Left() {return 0.0;} } class A { static void test() { typeof(TBounds.Left) m; } void test2() { typeof(TBounds.Left) m; } } void main() {} How can I fix this issue? Kind regards André |
July 26, 2017 Re: Why can't typeof() be used in member method? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Wednesday, 26 July 2017 at 13:51:05 UTC, Andre Pany wrote:
> How can I fix this issue?
I would just do
typeof((new TBounds).Left) m;
so then it is clear that you want a non-static member.
|
July 26, 2017 Re: Why can't typeof() be used in member method? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 26 July 2017 at 14:05:12 UTC, Adam D. Ruppe wrote: > On Wednesday, 26 July 2017 at 13:51:05 UTC, Andre Pany wrote: >> How can I fix this issue? > > I would just do > > typeof((new TBounds).Left) m; > > > so then it is clear that you want a non-static member. In my productive scenario I try to check whether a class (TLabel) has a specific member ("Margins.Left", "TextSettings.Font.Size"). mixin(`static if (!__traits(compiles, typeof(`~T.stringof~`.`~p.name~`))) { ... } This coding is within a member method and due to the issue with typeof it is failing. Creating new class for checking whether the class has a specific member is not possible in my scenario, as a DLL call is involved and I need the result (member available) as condition for static if. Due you think typeof should work within member methods and I should file an issue? Kind regards André |
July 26, 2017 Re: Why can't typeof() be used in member method? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Wednesday, 26 July 2017 at 16:50:35 UTC, Andre Pany wrote: > mixin(`static if (!__traits(compiles, typeof(`~T.stringof~`.`~p.name~`))) { > ... > } FYI, you shouldn't use .stringof here. Just use `T` instead of `T.stringof`. > Creating new class for checking whether the class has a specific member is not possible in my scenario, as a DLL call is involved and I need the result (member available) as condition for static if. This doesn't *actually* create a new class, it just compiles as if you would to make sure it has a valid `this` for it in the type system. You could also use `typeof(TBounds.init.Left)`, which is actually even better than `new` since it doesn't require the constructor arguments. But in either case, that code isn't actually run, it just looks for the non-static member function. > Due you think typeof should work within member methods and I should file an issue? I'm not sure... I could go either way on it since there is a reasonable answer here (int), but since it is non-static, trying to use it in an actual expression IS an error so it makes sense for typeof(error) to also be an error... |
July 26, 2017 Re: Why can't typeof() be used in member method? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 26 July 2017 at 17:04:59 UTC, Adam D. Ruppe wrote:
> On Wednesday, 26 July 2017 at 16:50:35 UTC, Andre Pany wrote:
>> [...]
>
> FYI, you shouldn't use .stringof here. Just use `T` instead of `T.stringof`.
>
> [...]
Thank you so much!
Kind regards
André
|
July 26, 2017 Re: Why can't typeof() be used in member method? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | On Wednesday, 26 July 2017 at 19:06:24 UTC, Andre Pany wrote:
> On Wednesday, 26 July 2017 at 17:04:59 UTC, Adam D. Ruppe wrote:
>> On Wednesday, 26 July 2017 at 16:50:35 UTC, Andre Pany wrote:
>>> [...]
>>
>> FYI, you shouldn't use .stringof here. Just use `T` instead of `T.stringof`.
>>
>> [...]
>
> Thank you so much!
>
> Kind regards
> André
There's another method for such a check.
mixin(`static if (!is(typeof(T.`~p.name~`))))
|
Copyright © 1999-2021 by the D Language Foundation