• Ephera@lemmy.ml
    link
    fedilink
    English
    arrow-up
    4
    ·
    1 day ago

    I can figure most of the new style out from context.

    • => is clearly a closure declaration operator, similar to JavaScript.
    • x ??= y is shorthand for “assign y to x if x is not set, and return x” which is kind of nice.

    Man, I’ve successfully stayed away from C# for a few years now, but that’s wild to me that the x ??= y operator would be intuitive to you.
    This could’ve easily been two or three operations, without being much more verbose, but actually being somewhat intuitively readable for most devs…

    • dejected_warp_core@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      edit-2
      1 day ago

      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.

      Edit: more context.