Why can't typeof() be used in member method?
Adam D. Ruppe via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 26 10:04:59 PDT 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...
More information about the Digitalmars-d-learn
mailing list