Does expression ignore immutability / const?
I am using a function template void _createAttr(T)(args..., in T[])
and am testing the type T
c 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 T and the supplied argument are not the same, but the modifier in
must take care of that. And in the second case, a conclusion should be drawn T = immutable(char)
. As far as I understand, immutable(char)
both char
are different types, but the compiler passes the test in the second case.
The compiler (DMD) seems to ignore the immutability of characters in the string when executing the test.
I couldn't find an explanation for this behavior on dlang.org or in the D Language Language book.
Is this a compiler error?
source to share
No mistake, this is just a qualifier in
, expandable up to const
, which is equally valid for both immutable(char)
, and for char
, so the compiler only creates a copy of it once.
If T == char
, then in T[]
means const char[]
that covers both cases, so the pattern never needs to think about immutability. You can also pass a volatile string to this function without any problem.
If you explicitly did !(immutable(char))
, then it will use that and no longer accept the modified one.
source to share