Well, I did have the older version on the left as a kind of rosetta stone for this. Plus, this kind of “init and/or return” pattern shows up a bunch of places, so it makes sense someone would want a quick version that’s harder to screw up or has fewer side-effects.
I’ve also spent years investigating better ways to do things through various versions of C++, D, Rust, Go, and TypeScript. After a while, the big-picture patterns start to emerge and you see different camps start to converge on the same kinds of things. Stuff like these weird features start to feel like learning a new slang term for something you’ve felt, but could never say so succinctly.
In the case of ??= it’s a more formalized Python x = x or y or x = x || y in JavaScript. The catch is that not all languages treat assignments like expressions that can be returned, so you get a clunky return as a separate statement; return (x = x or y) doesn’t always fly. That friction is all over the place, and it’s natural to want a shorthand for this very thing.
Sure enough, after searching a bit, ??= shows up in JS, PHP, and even Ruby has a version.
Well, I did have the older version on the left as a kind of rosetta stone for this. Plus, this kind of “init and/or return” pattern shows up a bunch of places, so it makes sense someone would want a quick version that’s harder to screw up or has fewer side-effects.
I’ve also spent years investigating better ways to do things through various versions of C++, D, Rust, Go, and TypeScript. After a while, the big-picture patterns start to emerge and you see different camps start to converge on the same kinds of things. Stuff like these weird features start to feel like learning a new slang term for something you’ve felt, but could never say so succinctly.
In the case of
??=
it’s a more formalized Pythonx = x or y
orx = x || y
in JavaScript. The catch is that not all languages treat assignments like expressions that can be returned, so you get a clunkyreturn
as a separate statement;return (x = x or y)
doesn’t always fly. That friction is all over the place, and it’s natural to want a shorthand for this very thing.Sure enough, after searching a bit,
??=
shows up in JS, PHP, and even Ruby has a version.Edit: more context.