How to do reflection on alias symbols

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Nov 17 09:42:39 UTC 2023


On Friday, November 17, 2023 2:11:30 AM MST Arafel via Digitalmars-d-learn 
wrote:
> I mean, in order to know if something is an `enum`, I need to do:
>
> ```d
> enum isEnum(alias a) = is(typeof(a)) && !is(typeof(&a));
> ```
>
> which feels like the wrong approach, and too much error-prone. I also
> fear I'm forgetting to consider some corner case.
>
> There is `is(E == enum)`, but it only works on types, and fails for
> anonymous enums, because `typeof` returns the base type.

Well, anonymous enums are what are called manifest constants, so they
literally aren't enums as far as their type goes, and the type system does
not consider them to be enums. They're just a way to declare constants
(they're essentialy the D equivalent of using #define for constants in
C/C++). Arguably, they should use a keyword other than enum (and that's been
debated in the past), but it's unlikely to change at this point.

So, if we were to add something to std.traits for them, it would probably be
something more like isManifestConstant than isEnum. In spite of the keyword
being used, they really aren't intended to be considered enums.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list