mixing (else) static if inadvertently with else if
kdevel
kdevel at vogtner.de
Sat May 18 12:00:05 UTC 2019
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?
More information about the Digitalmars-d-learn
mailing list