digitalmars.D.learn - Phobos func for string -> enum member?
- Nick Sabalausky (8/8) Oct 13 2016 I'm sure it'd be easy enough to write, but does phobos have a simple way...
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/15) Oct 13 2016 import std.conv;
- H. S. Teoh via Digitalmars-d-learn (7/18) Oct 13 2016 import std.conv : to;
- Nick Sabalausky (3/19) Oct 13 2016 Ah. Right. ;)
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.
Oct 13 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
Oct 13 2016
On Thu, Oct 13, 2016 at 07:11:15PM -0400, Nick Sabalausky via Digitalmars-d-learn 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 : to; Foobar a = "foo".to!Foobar; :-) T -- It is impossible to make anything foolproof because fools are so ingenious. -- Sammy
Oct 13 2016
On 10/13/2016 07:14 PM, H. S. Teoh via Digitalmars-d-learn wrote:On Thu, Oct 13, 2016 at 07:11:15PM -0400, Nick Sabalausky via Digitalmars-d-learn wrote:Ah. Right. ;) Thanks all.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 : to; Foobar a = "foo".to!Foobar; :-) T
Oct 13 2016