I has been using this pattern each time something needs special treatment when it can be null:
void doSomething(T)(T v)
{
import std.traits: isAssignable;
static if( isAssignable!(T, typeof(null))) {
if(v is null)
writeln("This is null");
else
writeln("This is not null");
} else {
writeln("This can't be null");
}
}
and then
void main(){
// output: This is null
doSomething!string(null);
// output: This is not null
doSomething("Hello");
// output: This can't be null
soSomething!int(1);
}
Problem appears with vibe-d
Json
.
void main(){
doSomething!Json(null);
}
Compiler outputs
Error: incompatible types for
(v) is (null):
Jsonand
typeof(null)`
-Why?
-Whats the correct whay to test if something can be null?