How to check if value is null, today?

Steven Schveighoffer schveiguy at gmail.com
Thu Oct 14 15:27:27 UTC 2021


On 10/14/21 7:58 AM, tastyminerals wrote:
> The new `DMD v2.097.2` deprecated implicit null conversions 
> `std.typecons.Nullable!double.Nullable.get_`.
> 
> The deprecation warning tell you to `Please use .get explicitly.`.
> 
> Here is an example code that doesn't work with the new compiler anymore:
> 
> ```
> if (someValue.isNull)
> ```
> 
> Attempting to run the above throws:
> ```
> Error: incompatible types for `(0) : (someValue)`: `int` and `Nullable!int`
> ```
> 
> I am not sure I understand what kind of `.get` overload are we supposed 
> to use here. I tried to read the documentation but looks like it is yet 
> to be updated.
> 
> Can somebody please help me out?

I think your expression is something more like:

```d
someValue.isNull ? 0 : someValue;
```

Due to the error message format. You need to call `get` explicitly to 
unwrap a Nullable now. So change it to:

```d
someValue.isNull ? 0 : someValue.get;
```

or even better:

```d
someValue.get(0);
```

which uses the default value of 0 if it's null.

-Steve


More information about the Digitalmars-d-learn mailing list