www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6921] New: Request for a 'static final switch' statement

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6921

           Summary: Request for a 'static final switch' statement
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: ThatsGobbles gmail.com



PST ---
I've often come across cases where I find it'd be handy to have a different
constructor based on a passed-in template enum parameter:

enum Options {
    Some, Many, All
}

class OptionManager(Options O) {
    static if(O == Options.Some) {
        alias TypeTuple!(int) TP;
    }
    else static if(O == Options.Some) {
        alias TypeTuple!(int, int, int) TP;
    }
    else static if(O == Options.Some) {
        alias TypeTuple!(int, int, int, int, int, int) TP;
    }
    else {
        static assert(0);
    }

    public this(TP params) {
        // ...
    }
}

However, this necessitates a static if-else chain, as shown. It would be quite
handy to have a static final switch variant for cases like the above. Would
this be something easy to implement in the D language?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 09 2011
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6921


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



Do you mean something like this? It's handy:


enum Options {
    Some, Many, All
}

class OptionManager(Options opt) {
    static final switch(opt) {
        case Options.Some:
            alias TypeTuple!(int) TP;
            break;
        case Options.Many:
            alias TypeTuple!(int, int, int) TP;
            break;
        case Options.All:
            alias TypeTuple!(int, int, int, int, int, int) TP;
            break;
    }

    public this(TP params) {
        // ...
    }
}

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