Safer enum casts
bearophile
bearophileHUGS at lycos.com
Tue Feb 1 12:32:50 PST 2011
If I have an enum of chars, and I have a variable that contains a generic char, I may want to convert the second to an instance of the first one, safely (a normal cast is enough to do it unsafely). Is it a good idea to give this purpose to to!() (this idea is currently not implemented)?
import std.conv: to;
enum Foo : char { a = 'A', b = 'B' }
void main() {
Foo r1 = cast(Foo)'A'; // OK
Foo r2 = cast(Foo)'X'; // error undetected
Foo r3 = to!Foo('A'); // OK
Foo r4 = to!Foo('X'); // error detected
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list