digitalmars.D.learn - Safer enum casts
- bearophile (11/11) Feb 01 2011 If I have an enum of chars, and I have a variable that contains a generi...
- Jesse Phillips (4/5) Feb 01 2011 I believe this is exactly what std.conv.to should do. It should be the s...
- bearophile (4/5) Feb 01 2011 http://d.puremagic.com/issues/show_bug.cgi?id=5515
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
Feb 01 2011
bearophile Wrote: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)?I believe this is exactly what std.conv.to should do. It should be the safest and recommended way to do conversions. I have a request in for preventing removal of qualifiers: http://d.puremagic.com/issues/show_bug.cgi?id=5307
Feb 01 2011
Jesse Phillips:I believe this is exactly what std.conv.to should do. It should be the safest and recommended way to do conversions.http://d.puremagic.com/issues/show_bug.cgi?id=5515 Bye, bearophile
Feb 01 2011