How to get BaseEnumType?

wobbles via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 31 01:10:34 PDT 2015


On Monday, 31 August 2015 at 07:55:53 UTC, drug wrote:
> Hello
> I need to get the type to which I can cast the enum for using 
> with foreign library. For example:
> ```
> enum Foo { A = "a", B = "b", }
> enum Bar { A = 123, B = 432, }
>
> static assert(is(BaseEnumType!Foo == string));
> static assert(is(BaseEnumType!Bar == int));
>
> I guess there is simple answer somewhere but I failed to find 
> it yet.

In std.traits there is the required isX funcitons.

import std.stdio;
import std.traits;
void main(){
         static if(isSomeString!Foo){
                 writefln("String: %s", Foo.A);
         }
         static if(isScalarType!Bar){
                 writefln("Integer: %s", Bar.A);
         }
         static if(isSomeString!Bar){
                 writefln("String: %s", Foo.A); // wont print
         }
         static if(isScalarType!Foo){
                 writefln("Integer: %s", Bar.A); // wont print
         }
}

enum Foo { A="a", B = "b" }
enum Bar { A=1, B=2 }



More information about the Digitalmars-d-learn mailing list