Bug 22293 – Nullable should define opCast!bool

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-09-09T20:33:31Z
Last change time
2024-08-01T09:09:44Z
Keywords
pull
Assigned to
No Owner
Creator
Steven Schveighoffer

Comments

Comment #0 by schveiguy — 2021-09-09T20:33:31Z
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.
Comment #1 by dlang-bot — 2024-07-31T18:44:11Z
@pbackus created dlang/phobos pull request #9039 "Fix Bugzilla 22293: opCast!bool for Nullable" fixing this issue: - Fix Bugzilla 22293: opCast!bool for Nullable The second opCast overload is necessary to allow built-in implicit conversions (e.g., mutable to const) to be performed explicitly via a cast expression. Nullable.opEquals depends on this behavior. https://github.com/dlang/phobos/pull/9039
Comment #2 by dlang-bot — 2024-08-01T09:09:44Z
dlang/phobos pull request #9039 "Fix Bugzilla 22293: opCast!bool for Nullable" was merged into master: - fc83d371c21ca1b1f71d6dc3e6dde13b0de50ab9 by Paul Backus: Fix Bugzilla 22293: opCast!bool for Nullable The second opCast overload is necessary to allow built-in implicit conversions (e.g., mutable to const) to be performed explicitly via a cast expression. Nullable.opEquals depends on this behavior. https://github.com/dlang/phobos/pull/9039