On 03/27/2013 12:08 PM, Steven Schveighoffer wrote:
On Wed, 27 Mar 2013 11:34:19 -0400, Vidar Wahlberg
<vidar.wahlb...@gmail.com> wrote:

- When casting a value to an enum, there's no checking that the value
actually is a valid enum value. Don't think I ever found a solution on
how to check whether the value after casting is a valid enum value, it
hasn't been a pressing issue.

Typically, one uses std.conv.to to safely convert one value into
another. Cast should be avoided unless absolutely necessary.

I just tested it, it works on enum *strings*, but not enum *values*

For example:

import std.conv;

enum X {
i, j, k
}

void main()
{
X x = to!X("i"); // works!
x = to!X(1); // fails!
}


I think to should be able to do this, but I'm not a good enough guru
with templates and compile-time type info to know if it's possible.
Anyone know if this is possible? If so, I think it should be added.

-Steve

It also works on values....


enum A
{
    B,
    C,
    D,
}
int w;

import std.conv;
import std.stdio;

void main()
{
   auto t = w.to!A.B;

   writeln(t);
}

Reply via email to