immutability with var and def #1234
-
I have a probably dumb question about immutability in Janet. I understand I define immutable data with So what is this?
What is that really and are And another question, whats the difference?
If I can |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You're thinking of two different meanings of mutable and immutable. Let's start by explaining (im)mutable bindings. (def a 5)
(defn pa [] (print a))
(pa) # => prints 5 This works because the (def a 5)
(defn pa [] (print a))
(def a 6)
(pa) # => still prints 5
(var a 5)
(defn pa [] (print a))
(set a 6)
(pa) # => prints 6 This is because we modified the binding. So what's that about the data then? (def a {:key :value})
(defn pa [] (print (a :key)))
(pa) # => prints value Let's say we wanted it to print (def a @{:key :value})
(defn pa [] (print (a :key)))
(put a :key :newvalue)
(pa) # => prints newvalue Hopefully that clears it up. |
Beta Was this translation helpful? Give feedback.
It is different.
In the case of a, you have an immutable binding with mutable data.
In the case of b, you have a mutable binding with mutable data.
The mutability of the binding vs the data really have nothing to do with one another.
Here is another example:
On the converse, the same with mutable bindings: