optional assignment

Hipreme msnmancini at hotmail.com
Wed Feb 26 09:46:28 UTC 2025


On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:
> A common pattern in a world where `null` exists is
>
> ```d
> if (!a)
>   a = b;
> ```
>
> that is semantically equivalent, if `a` is a pointer (or if it 
> implements opCast!bool), to D
>
> ```d
> if (a is null)
>   a = b;
> ```
>
> I propose the get rid of the statement layer. The two statments 
> (there are more actually) can be a single expression:
>
> ```d
> a ?= b;
> ```
>
> As a new binary assign operator.

Currently, I've been using std for that.

```d
import std.algorithm.comparison;
import std.stdio;

void main()
{
     int a;
     a = either(a, 500);
     writeln = a;
}
```


More information about the dip.ideas mailing list