[Issue 10253] New: Switch and Final Switch do not work with subtyping

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jun 3 07:00:43 PDT 2013


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

           Summary: Switch and Final Switch do not work with subtyping
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrej.mitrovich at gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-06-03 07:00:42 PDT ---
-----
struct Wrapper(E) if (is(E == enum))
{
    E value;
    alias value this;
}

void main()
{
    enum E { a, b }

    alias Wrapper!E WE;
    WE we;

    switch (we)  // L14
    {
        case WE.a: break;
        case WE.b: break;
        default:
    }

    final switch (we)  // L21
    {
        case WE.a: break;
        case WE.b: break;
    }
}
-----

test.d(14): Error: 'we' must be of integral or string type, it is a Wrapper!(E)
test.d(16): Error: cannot implicitly convert expression (cast(E)0) of type E to
Wrapper!(E)
test.d(17): Error: cannot implicitly convert expression (cast(E)1) of type E to
Wrapper!(E)
test.d(21): Error: 'we' must be of integral or string type, it is a Wrapper!(E)
test.d(23): Error: cannot implicitly convert expression (cast(E)0) of type E to
Wrapper!(E)
test.d(24): Error: cannot implicitly convert expression (cast(E)1) of type E to
Wrapper!(E)

The use-case is the ability to define an enum wrapper which disables
default-initialization of the enum and requires explicit initialization, for
example:

-----
struct ExplicitEnum(E) if (is(E == enum))
{
    @disable this();
    this(E e) { value = e; }

    E value;
    alias value this;
}

///
unittest
{
    /**
        The library writer would typically disallow access to
        this enum so it can never be directly used in user-code.
    */
    /* private */ enum MachineEnum
    {
        X86,
        X86_64,
    }

    // the fake enum type
    alias Machine = ExplicitEnum!MachineEnum;

    static assert(!__traits(compiles,
    {
        Machine machine;  // compile-time error
    }()));

    Machine machine = Machine.X86;  // ok
}
-----

The only missing puzzle piece is the ability to use a switch/final switch on
such a wrapper type.

-- 
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