[Issue 10490] New: Type enum in std.variant.Algebraic for final switches

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jun 27 13:48:49 PDT 2013


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

           Summary: Type enum in std.variant.Algebraic for final switches
           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 2013-06-27 13:48:47 PDT ---
This enhancement request is just an idea. Maybe better ideas can be found.

Given the definition of an Algebraic like this:

import std.variant;
struct Foo {}
void main() {
    alias T = Algebraic!(int, Foo);
}



I think it can be a good idea to generate automatically inside T an enum and a
method like this:

enum Type { int_, Foo; }

@property Type theType() const { return ...; }

(Built-in types are keywords so they get a leading underscore.)


So an Algebraic can be used in a final switch:

import std.variant;
struct Foo {}
void main() {
    alias T = Algebraic!(int, Foo);
    auto t = T(5);
    final switch (t.theType) {
        case T.Type.int_:
            auto p = t.peek!int;
            break;
        case T.Type.Foo:
            auto p = t.peek!Foo;
            break;
    }
}


(Currently T.type is a TypeInfo, that can't be used in a final switch.)

This allows a poor man's safe pattern matching on an Algebraic.

An implementation of Issue 596 will allow to use a bit better pattern matching
on an Algebraic.

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