Suppose I have the following variadic function, which job is to concat together a path from pieces (each piece can either be an index of integral type, or a node of string type):
string
makePath(P...)(P path)
{
import std.conv;
import std.format;
string res;
foreach (piece; path) {
pragma(msg, typeof(piece).stringof);
static if (is(piece: size_t))
res = res is null ? piece.to!string : format("%s[%s]", res, piece);
else if (is(piece: string))
res = res is null ? piece : format("%s.%s", res, piece);
else
static assert(0);
}
}
If I later use it like this: string path = makePath("foo", "bar"), somehow the code reaches the static assert(0); and the compilation terminates. This is most curious, but the pragma actually writes string as the type of the first argument despite the fact that a code path for some other type was taken.
Even better, using makePath(12, 13) results in the compiler complaining about both the line for strings (about incompatible types of int and string) and static assert. What is going on here?
I've tried this both on DMD and LDC.
else ifshould prolly beelse static if. Is that a mistake in your original code or just here? - Adam D. Ruppe