[Issue 5515] New: std.conv.to for safer enum casts

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Feb 1 16:24:47 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5515

           Summary: std.conv.to for safer enum casts
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-02-01 16:22:32 PST ---
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.

A normal cast is enough to do it unsafely, but I'd like to be allowed to use
to!() to convert it safely (it may raise an error if it's not among the enum
cases).

An example:


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
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list