digitalmars.D.learn - mixing (else) static if inadvertently with else if
- kdevel (52/52) May 18 2019 Today I stumbled over this error:
- rikki cattermole (32/32) May 18 2019 Works:
Today I stumbled over this error: elsestaticif.d ``` import std.stdio; void insert () () { // some code } void insert (T, Args ...) (T x, Args args) { static if (T.stringof == "int") {{ `int`.writeln; }} else if (T.stringof == "bool") {{ // ← "static" is missing here `bool`.writeln; }} else static if (T.stringof == "string") {{ `string`.writeln; }} else {{ static assert (false); }} return insert (args); } void main () { insert (1, true, "string"); } ``` $ dmd elsestaticif elsestaticif.d(20): Error: static assert: false is false elsestaticif.d(22): instantiated from here: insert!(bool, string) elsestaticif.d(27): instantiated from here: insert!(int, bool, string) What confuses me is that D allows non-static else mixed with static else if clauses. I found this bug: https://issues.dlang.org/show_bug.cgi?id=1053 Issue 1053 - Make 'static else if' or 'static if (...) {...} else if' work which states: On the other hand, I'm pretty sure static if (foo) { } else if (bar) { } is an error currently, so it would be nice if that were just taken to be a "static else if". There isn't a "static else {}" so why should we have to repeat the "static" on an "else if"? One "static" should be enough. That sounds reasonable. Is there any further discussion on this problem?
May 18 2019
Works: --- import std.stdio; void main() { func!true(false); func!false(false); func!false(true); } void func(bool A)(bool b) { static if (A) { writeln("a"); } else if (b) { writeln("b"); } else { writeln("c"); } } --- This works because that static if + if + else actually is: --- static if (A) { ... } else { if (B) { ... } else { ... } } --- This works as intended.
May 18 2019