Phobos func for string -> enum member?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 13 16:15:58 PDT 2016


On 10/13/2016 04:11 PM, Nick Sabalausky wrote:
> I'm sure it'd be easy enough to write, but does phobos have a simple way
> to convert the name of an enum member from a runtime string to the enum
> type?
>
> Ie, something like:
>
> enum Foobar { foo=1, bar }
> Foobar a = doesThisExistInPhobos!Foobar("foo");
>
> I'm not finding anything like it in the docs, but I'm wondering if I
> just missed it somewhere.

import std.conv;

enum Foobar { foo=1, bar }
void main() {
     assert("foo".to!Foobar == Foobar.foo);
}

Ali



More information about the Digitalmars-d-learn mailing list