I'm using a function template void _createAttr(T)(args..., in T[])
and testing the type of T
with static if(is(T == char))
in the function. When I call,
_createAttr!char(args...,"someString")
_createAttr(args...,"someString")
the compiler never complains.
Of course I know that alias string = immutable(char)[]
. So in the first call the type of T and the supplied argument don't match, but the in
modifier should take care of that. And in the second case it should infer T = immutable(char)
. As I understand it, immutable(char)
and char
are distinct types, but the compiler passes the is test in the second case.
The compiler (DMD) seems to ignore the immutableness of the chars in the string when doing the is test.
I couldn't find any explanation for this behavior on dlang.org or in The D Programming Language book.
Is this a compiler bug?
No bug, it is simply the in
qualifier expanding to const
, which is equally valid for both immutable(char)
and char
, so the compiler only instantiates it once.
If T == char
, then in T[]
means const char[]
which covers both cases so the template never needs to think about immutability. You could also pass a mutable string to that function without any problems.
If you explicitly did !(immutable(char))
then it would use that, and no longer accept the mutable one.