What is the rationale behind enabling Nullable!T to be used as a regular T?
Adnan
relay.public.adnan at outlook.com
Fri Feb 14 08:54:41 UTC 2020
Nullable!T, as I understand is somewhat similar to Rust's
Option<T>. It's meant to be an alternative to sentinel value
based error checking.
In regular Rust code if you do something like
let x: Option<i32> = None;
println!("{}", x + 4);
It should throw compile error, since the `x` in `x + 4` is not
checked for null. OpAdd<rhs = i32> is not implemented for
Option<i32> for that reason.
Of course, you can get the value using the unwrap() method, which
would create runtime crash for null option. However when
typing-out "unwrap()" you understand what you are signing up for.
Back to D.
const Nullable!int a;
assert(a.isNull);
writeln(a + 4); // compiles with 0 warnings
Why is opBinary implemented for Nullable!T? Doesn't it defeat its
purpose?
More information about the Digitalmars-d
mailing list