First Draft: opUnwrapIfTrue
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Thu May 7 23:38:34 UTC 2026
On 08/05/2026 12:44 AM, Per Nordlöw wrote:
> On Thursday, 26 February 2026 at 13:53:41 UTC, Richard (Rikki) Andrew
> Cattermole wrote:
>> The DIP: https://gist.github.com/
>> rikkimax/21242118e3bc1bf5f28024c2cdc33557
>
> I really want this. I believe a new distinct syntax that triggers the
> call to `opUnwrapIfTrue` is needed. My suggestion is something that
> involves the question mark symbol. For instance,
>
> ```d
> if (auto value ?= result) {}
> ```
>
> or
>
> ```d
> if (auto value =? result) {}
> ```
>
> . Alternatively, what about extending the `foreach` to allow
>
> ```d
> foreach (auto value; result)
> ```
You need the else branch of an if statement for this feature to be used
in all cases.
Foreach statements are non-starters.
> when
>
> ```d
> typeof(result).opUnwrapIfTrue
> ```
>
> exists? A bit longer to type, though.
What I came up with is to require a call to that function i.e. through
an alias called say unwrap.
```d
if (auto v = wrapper.unwrap)
```
However I want to disallow the call to opUnwrapIfTrue in any other
circumstances and I have no found a way to do so that makes me happy.
Over all I think its better to drop this usage syntax requirement entirely.
No other language has it.
More information about the dip.development
mailing list