5 ms·
No, it only means that you can reassign the name `a` to a different value (i.e. it is a variable, not a constant). It would be mutable if it allowed something
by abiogenesis 6y ago
No, it only means that you can reassign the name `a` to a different value (i.e. it is a variable, not a constant).
It would be mutable if it allowed something like this:
> a = 5
> a.add(1)
> print a
6
Note that numbers are immutable in most languages anyway. Arrays, hashmaps, and sometimes strings are the data types that are frequently mutable.
- jedberg 6y agoI suppose you could explain it that way. But given that it's not an object oriented language, how could any variable be mutable given your example/definition? How could you mutate a variable in a way that this language disallows?
- ben-schaaf 6y ago> How could you mutate a variable in a way that this language disallows? I presume something like this: array a = [1, 2, 3] a.append(4) Names are mutable, but the values they point to are not.
- cnasc 6y agoI think something more like a[0] = 1 Would be a better example. Appending to an array could involve creating a new array and reassigning it to ‘a’.
- catlifeonmars 6y agoThe getting started guide demonstrates that arrays are mutable. https://chaos-lang.org/docs/05_arrays https://chaos-lang.org/docs/05_arrays
- abiogenesis 6y agoYet it says [1]: > Arrays and the elements of arrays are also immutable I guess the docs need a bit of rewording. Forcing a deep-copy on variable assignment does not mean the variable itself is immutable. [1] https://chaos-lang.org/docs/07_immutability https://chaos-lang.org/docs/07_immutability
- catlifeonmars 6y agoAh, I did not dive that deep. Now I am curious what the impact of deep copies on performance is, and if the language employs any clever optimizations to improve performance.
- abiogenesis 6y agoI don't see how you can reliably deep copy any user defined class either.
- lgessler 6y agoThere are none, at least in the familiar OOP sense: > Chaos language is not object-oriented. So everything is done by functions and data types.
- deleted 6y ago[deleted]
- agentultra 6y agoTurns out that immutable data structures can share a lot of structure safely. https://www.amazon.com/Purely-Functional-Structures-Chris-Okasaki-ebook/dp/B00AKE1V04 https://www.amazon.com/Purely-Functional-Structures-Chris-Ok...
- catlifeonmars 6y agoAs an aside, rebinding local references can be viewed as a form of mutability.
- deleted 6y ago[deleted]
- eyelidlessness 6y agoNumbers are literally never immutable in any language that isn't designed intentionally for comedy. Variables of a number type may be mutable, but the numbers themselves never mutate. Strings are typically immutable in most languages, but that does vary some (generally surprising people who expect primitives to be immutable).
- stellalo 6y agoFor a moment I started imagining what a mutable number would be like: > 3 = 5 > print 3 5 The end
- tralarpa 6y agoYou can kind of do that in Java by manipulating the integer cache
- mqus 6y agoPythons numbers aren't as immutable as you think ;) https://kate.io/blog/2017/08/22/weird-python-integers/ https://kate.io/blog/2017/08/22/weird-python-integers/
- pmontra 6y agoThat's standard behavior for object oriented languages. A different example with functions: function add(x, y) { return x + y } a = 5 add(a, 1) print(a) A language printing 6 is pass by reference. A language printing 5 is pass by value. Both would print 6 for this code a = add(a, 1) print(a) However a language like Erlang would end with an error when trying to reassign a new value to a.
- pmontra 6y agoI correct myself. The function in the first example should be function add(x, y) { x = x + y return x }