www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22293] New: Nullable should define opCast!bool

https://issues.dlang.org/show_bug.cgi?id=22293

          Issue ID: 22293
           Summary: Nullable should define opCast!bool
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: schveiguy gmail.com

Now that Nullable.get is not alias-this, it would make sense to make
if(someNullable) work, instead of always having to do if(!someNullable.isNull),
which is harder to read (the ! appears at the beginning, of a possibly long
expression chain, plus its a negation which is always harder).

A bonus is that if(auto x = expressionReturningNullable) will work as well.

To do this, we just have to add opCast(T: bool) { return !isNull; } to the
type.

Not sure on the situation with Nullable with a null value, since that still has
the alias-this.

--
Sep 09 2021