www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to check if value is null, today?

reply tastyminerals <tastyminerals gmail.com> writes:
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?
Oct 14 2021
next sibling parent reply jfondren <julian.fondren gmail.com> writes:
On Thursday, 14 October 2021 at 11:58:29 UTC, tastyminerals wrote:
 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`
 ```
Do you have a complete example? Because this runs without error: ```d import std.typecons : nullable, Nullable; import std.stdio : writeln; void main() { auto a = nullable(1); auto b = Nullable!int.init; if (!a.isNull) writeln(a.get); if (b.isNull) writeln("b is null"); } ```
Oct 14 2021
parent reply tastyminerals <tastyminerals gmail.com> writes:
On Thursday, 14 October 2021 at 12:43:36 UTC, jfondren wrote:
 On Thursday, 14 October 2021 at 11:58:29 UTC, tastyminerals 
 wrote:
 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`
 ```
Do you have a complete example? Because this runs without error: ```d import std.typecons : nullable, Nullable; import std.stdio : writeln; void main() { auto a = nullable(1); auto b = Nullable!int.init; if (!a.isNull) writeln(a.get); if (b.isNull) writeln("b is null"); } ```
Steven Schveighoffer was correct, the error was caused by non explicit `someVar;` which had to be changed to `someVar.get;`.
Oct 15 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 10/15/21 6:39 AM, tastyminerals wrote:
 On Thursday, 14 October 2021 at 12:43:36 UTC, jfondren wrote:
 Do you have a complete example? Because this runs without error:
 
 Steven Schveighoffer was correct, the error was caused by non explicit 
 `someVar;` which had to be changed to `someVar.get;`.
As a note for future reference, as jfondren says it's always good to post the exact code that is failing, better yet a complete example we can run. Often times people (including myself and many long-time users) come here puzzled about some message, but we have focused our brain on something that we think is causing the problem, but it isn't. Just posting that small piece brings us into your focused confusion instead of really looking at the big picture ;) -Steve
Oct 15 2021
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Oct 14 2021