It makes the code icky and hard to debug, and you can simply return new immutable objects for every state change.

EDIT: why not just create a new object and reassign variable to point to the new object

  • Giooschi@lemmy.world
    link
    fedilink
    English
    arrow-up
    0
    ·
    18 days ago

    What you need here is not the stability in memory (i.e. of pointers, which you lose when you recreate an object) but instead just the stability of an identifier (e.g. the index into a list).

    • tunetardis@lemmy.ca
      link
      fedilink
      English
      arrow-up
      1
      ·
      18 days ago

      Well, but then you’re basically just pushing the mutability onto the container, since you need to be able to replace elements within it.

      It’s a good strategy at times though. Like say you’re working in a language where strings are immutable and you want a string you can change. You can wrap it in a list along the lines s=['foo'] and pass references to the list around instead. Then if you go s[0]='bar' at some point, all the references will now see ['bar'] instead.