struct None{};
struct Some(T){
T value;
}
Optional!T some(T)(T value){
return Optional!T(Some!T(value));
}
bool isNone(T)(ref Optional!T optional){
return optional.peek!None != null;
}
bool isSome(T)(ref Optional!T optional){
return optional.peek!(Some!T) != null;
}
Optional!T none(T)(){
return Optional!T(None());
}
alias Optional(T) = std.variant.Algebraic!(Some!(T),None);
void main()
{
Optional!int si = none!int();
writeln(isNone!int(si));
writeln(isSome(si)); //cannot deduce function from argument types
}
为什么我必须使用显式类型调用isSome
和isNone
? D不应该能推断出类型吗?
我想这是因为Optional(T)
只是一个类型别名?