Bug 6921 – Request for a 'static final switch' statement
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-11-09T11:28:00Z
Last change time
2016-10-15T01:09:06Z
Assigned to
nobody
Creator
ThatsGobbles
Comments
Comment #0 by ThatsGobbles — 2011-11-09T11:28:40Z
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?
Comment #1 by bearophile_hugs — 2012-05-27T05:54:55Z
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) {
// ...
}
}
Comment #2 by TeddyBear12311 — 2016-08-05T22:42:08Z
It would also be nice to have a static final switch on a type tuple. Essentially every type in the type tuple must have an equivalent case. I've ran across a few bugs in my code due to the fact that I forgot a static if (T == sometype). It just fails silently. This happens when new types are added.