On Monday, 30 December 2024 at 15:07:41 UTC, Basile B. wrote:
> A common pattern in a world where null
exists is
if (!a)
a = b;
that is semantically equivalent, if a
is a pointer (or if it implements opCast!bool), to 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:
a ?= b;
As a new binary assign operator.
C# has this as the ??
and ??=
operator. I use it sometimes and it’s handy. I don’t care much about its exact syntax (some will suggest ?:
for the binary one, but ??=
looks a lot better than ?:=
IMO).
It should be overloadable, too: opBinary!"??"
and opBinaryRight!"??"
and opOpAssign!"??"
should worl as intended.
It’s also an easy way to chicken out of null sutff:
class C;
C f(int x);
C g(int x);
int n = 42;
C c = f(n) ?? g(n) ?? throw new InvalidOperationException();