• @[email protected]
    link
    fedilink
    10
    edit-2
    27 days ago

    No mutable types? So like… no lists? no for … i++?

    I get that there are alternative approaches, but I don’t quite see why you’d want to go to that extreme with this idea? It’s useful for some applications but even for a simple video game it’s likely not helpful.

    • @[email protected]
      link
      fedilink
      1227 days ago

      It’s perfectly possible to work without mutability.

      Is it desirable to be entirely without it? Probably not, but leaning immutable is definitely beneficial.

      • @[email protected]
        link
        fedilink
        1
        edit-2
        27 days ago

        Yeah, copying can totally screw you over. Mutability is fine, just make sure it’s safe (e.g. what Rust does).

      • @[email protected]
        link
        fedilink
        3
        edit-2
        27 days ago

        I get that there are alternative approaches, but I don’t quite see why you’d want to go to that extreme with this idea? It’s useful for some applications but even for a simple video game it’s likely not helpful.

        I should’ve said that right away, really. That’s on me being online while tired. At that time I did not really think outside the box I was working in that day

        • socsa
          link
          fedilink
          English
          327 days ago

          It’s just a very common foot gun, especially in legacy code where it is not explicit in the design. Even when you have proper getters and setters, it’s way to easy for someone to overload the scope of some object, either intentionally or accidentally and modify it inappropriately.

          • @[email protected]
            link
            fedilink
            127 days ago

            I suppose immutability is a solution, I’m not sure if it’s a good idea to radically isolate everything though

            • @[email protected]
              link
              fedilink
              126 days ago

              it’s not radical, it’s just a guarantee that if you hold a reference to an object, it won’t change state under you. It’s a bit like every object has MVCC and copy-on-write semantics built in.

              It’s easy enough to edit the object, producing a new copy, and then explicitly store it back where it goes or send it to whatever consumer needs it.

              • @[email protected]
                link
                fedilink
                126 days ago

                I get the idea, and how you keep it from copying a lot of data unnecessarily. A radical approach would be using immutable types exclusively

                • @[email protected]
                  link
                  fedilink
                  126 days ago

                  I guess, as a Scala enthusiast, it’s second nature to me - Scala incorporates immutable-by-default into its design so there are accommodations for it (.copy() methods on case classes, well-thought-out operators and methods on collections, “val” bindings, expression-oriented syntax).

                  It also lets you have normal OO classes and mutable vars anytime you want them, so you’re not stuck in a corner like you may sometimes be in Haskell if you don’t know the applicable FP pattern.

                • @[email protected]
                  link
                  fedilink
                  122 days ago

                  Oh, regarding copying data - immutable collections are based on https://en.m.wikipedia.org/wiki/Persistent_data_structure - when a change is applied, you get back a reference to a new data structures where as many inner references as possible are shared with the old one. So, all the parts that didn’t change, are not copied.

                  For something like a Scala case class (similar to a record), o.copy(membername1 = newvalue) returns a new object, with a new membername1 reference, but all other member references are the same as the copied-from object. So it’s a shallow copy with minimal changes.

                  you might see how default immutability as a policy makes this more predictable and able to be reasoned about - any mutable object in an object graph that has a shared reference in a copy may surprise you by suddenly changing state.

                  Of course, that’s the situation everywhere, all the time, in default-mutable languages. How many people set a default value of a Python function argument to [] or {} and were baffled when things started breaking because the instance of the default value was mutated?

                  • @[email protected]
                    link
                    fedilink
                    122 days ago

                    Clever! But I’d worry to run into performance problems when some operations effectively require copying or becoming a sort of linked list.

                    Although I suppose you could also be explicit if you do need it to behave in a particular way.

                    I like it!

      • @[email protected]
        link
        fedilink
        327 days ago

        Or pragmatic functional programming, or rediscovered by “OO” programmers who realize they are messing up the Redux store bad.

    • @[email protected]
      link
      fedilink
      227 days ago

      Erlang/Elixir doesn’t have muteable variables/types. Appending to a list would just create a “new” lists.