digitalmars.D.learn - Why can't typeof() be used in member method?
- Andre Pany (28/28) Jul 26 2017 Hi,
- Adam D. Ruppe (4/5) Jul 26 2017 I would just do
- Andre Pany (17/22) Jul 26 2017 In my productive scenario I try to check whether a class (TLabel)
- Adam D. Ruppe (15/25) Jul 26 2017 FYI, you shouldn't use .stringof here. Just use `T` instead of
- Andre Pany (4/9) Jul 26 2017 Thank you so much!
- Temtaime (3/14) Jul 26 2017 There's another method for such a check.
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é
Jul 26 2017
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.
Jul 26 2017
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: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é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.
Jul 26 2017
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...
Jul 26 2017
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:Thank you so much! Kind regards André[...]FYI, you shouldn't use .stringof here. Just use `T` instead of `T.stringof`. [...]
Jul 26 2017
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:There's another method for such a check. mixin(`static if (!is(typeof(T.`~p.name~`))))On Wednesday, 26 July 2017 at 16:50:35 UTC, Andre Pany wrote:Thank you so much! Kind regards André[...]FYI, you shouldn't use .stringof here. Just use `T` instead of `T.stringof`. [...]
Jul 26 2017