My thoughts & experiences with D so far, as a novice D coder

Steven Schveighoffer schveiguy at yahoo.com
Wed Mar 27 10:08:19 PDT 2013


On Wed, 27 Mar 2013 11:34:19 -0400, Vidar Wahlberg  
<vidar.wahlberg at 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


More information about the Digitalmars-d mailing list