Thread overview |
---|
August 31, 2015 How to get BaseEnumType? | ||||
---|---|---|---|---|
| ||||
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. |
August 31, 2015 Re: How to get BaseEnumType? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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 }
|
August 31, 2015 Re: How to get BaseEnumType? | ||||
---|---|---|---|---|
| ||||
Posted in reply to wobbles | On Monday, 31 August 2015 at 08:10:35 UTC, wobbles wrote:
> On Monday, 31 August 2015 at 07:55:53 UTC, drug wrote:
>
> 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 // Bar.A
> }
> static if(isScalarType!Foo){
> writefln("Integer: %s", Bar.A); // wont print // Foo.A
> }
> }
>
> enum Foo { A="a", B = "b" }
> enum Bar { A=1, B=2 }
My copy paste got the better of me. The print messages in the last two static ifs shoudl be Bar.A and Foo.A respectively :)
|
August 31, 2015 Re: How to get BaseEnumType? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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.
Use
is(std.traits.OriginalType!Foo == string)
maybe?
|
August 31, 2015 Re: How to get BaseEnumType? | ||||
---|---|---|---|---|
| ||||
Posted in reply to wobbles | On 31.08.2015 11:10, wobbles wrote:
>
> 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 }
>
Thank you. But is there a way to check against not predefined type?
|
August 31, 2015 Re: How to get BaseEnumType? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enamex | On 31.08.2015 11:12, Enamex wrote:
> 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.
>
> Use
> is(std.traits.OriginalType!Foo == string)
> maybe?
Yes, it works, thank you!
|
Copyright © 1999-2021 by the D Language Foundation