First Draft: opUnwrapIfTrue

Meta jared771 at gmail.com
Thu Feb 26 18:58:41 UTC 2026


On Thursday, 26 February 2026 at 18:03:31 UTC, Dennis wrote:
> On Thursday, 26 February 2026 at 13:53:41 UTC, Richard (Rikki) 
> Andrew Cattermole wrote:
>> ```D
>> if (int value = result) {
>>     // got a value!
>>     assert(value == 99);
>> } else {
>>     // oh noes an error or default init state
>> }
>> ```
>
> So after this DIP, the value in the if-condition can now be 
> falsey.
>
> ```D
> if (int value = result)
> {
>     // value can be 0 here
>     assert(value); // can fail!
> }
> ```
>
> This looks very confusing to me. Note that Rust's `if let 
> Some(value) = result
> ` doesn't have this problem because the `Some()` makes it 
> explicit that unwrapping is going on. (Also int/i32 doesn't 
> implicitly convert to bool there).
>
> You list range primitives as prior work, but notably they 
> require the dedicated foreach keyword instead of being an 
> invisible overload of the old `for` statement. I think if we 
> add unwrapping or pattern matching to D, it should have its own 
> syntax.

Agreed; D already has limited pattern matching in template type 
lists and is-expressions anyway. If we're going to do something 
like this DIP, we should just instead add a new form of if 
statement or add pattern matching. There was a DIP years ago I 
think that proposed a very simple mechanism for adding custom 
unpacking functionality to types.


More information about the dip.development mailing list