The problem with Nullable: nullable(null).isNull is false.

Piotr Mitana piotr.mitana at e.email
Mon Jul 25 12:26:29 UTC 2022


On Monday, 25 July 2022 at 12:04:35 UTC, Timon Gehr wrote:
> Not a big fan. E.g., this ad-hoc special case would make it 
> hard to use Nullable reliably in generic code.

Could you come up with an example of use case that would be 
problematic?

I perceive Nullable as a way to add a null value to the type that 
does not have it by default (for example `Nullable!int` etc.). As 
classes and reference types already do have their `null`, The 
Nullable in a general case would serve as "add the null *if if 
there isin't one*. The `Nullable!int` and `Nullable!MyClass` both 
would have a single null value, which I believe will be even more 
convenient to use in a generic code.

Also, a [library with an Optional 
type](https://code.dlang.org/packages/optional) has been written 
and in its README it actually states the problem that I have 
raised here. With Nullable covering this case there would be no 
need for this library, as Phobos' solution would be a proper 
solution for this problem.


More information about the Digitalmars-d mailing list