The problem with Nullable: nullable(null).isNull is false.
Piotr Mitana
pm at example.com
Mon Jul 25 17:12:20 UTC 2022
On Monday, 25 July 2022 at 13:05:08 UTC, Steven Schveighoffer
wrote:
> A counter example:
>
> ```d
> Nullable!C getDBValue(int key);
>
> auto x = getDBValue(42);
> if(x.isNull) { writeln("database does not contain the requested
> row"); }
> else if(x.get is null) { writeln("database contains the
> requested row, but the value is set to NULL"); }
> ```
>
> The real "problem" is that Nullable is called "Nullable".
>
> -Steve
You're right, this case justifies double-checking, as there are
two ways of not having a real value (either the given key is
absent or the value for it is `null`). Still, this is a somewhat
specific case.
But that indeed means that both these options should remain
available. Maybe an alias could be added then to differentiate it
– something like
```d
alias SmartNullable(T)(T t) = Nullable!(T, null)
```
(probably with a different name & aliasing to the plain nullable
for primitives and structs).
More information about the Digitalmars-d
mailing list