Anyone want to put in your recommendation as per t...
# getting-started
e
h
Hi, There is basically 2 types of immutability; Referential immutability and Immutable values. Referential immutability refers to the reference being immutable and not the object itself. In the following example: “val mutableList” The mutableList reference is immutable because of “val”. This means you if you instantiate it like this: “val mutableList = mutableListOf(1,2,3,4)“, You cannot reassign it like this: “mutableList = mutableListOf(5,6,7,8)“. However, you would still be able to mutate the list assigned to “mutableList” like this: “mutableList.add(10)” This is known as Referential immutability. On the other hand, with: “var immutableList = listOf(1,2,3,4)“, You would be able to reassign the object bound to the variable “immutableList”. Therefore, you would be able to do things like this: “var immutableList = listOf(1,2,3,4) immutableList = listOf(5,6,7,8)” However, you would not be able to mutate the object itself like this: “immutableList.add(10)” This type of immutability is known as Immutable values. So to answer your question, if you need to mutate properties within an object but not the object itself, stick with “val”, and if you need to assign new objects to the same variable and not mutate properties within that variable, then use “var” (As an aside, I honestly cannot think of a use case where this would be necessary). Obviously you can go down the road of “var mutableList”, but with that you lose all the benefits of immutability 🙂 I hope you find that helpful. Reference: Functional Kotlin (Chapter 3)
👍 1
e
Thanks @Hasan. There are some people recommend to use immutable list, and var instead when changes is needed. At a first glance I think that make sense, as immutable is like the preferred way of doing things. But after thinking though, it needs a
var
, which makes entire list mutable, so confuse of which is better.
h
@elye, are you working on a particular problem where you encountered this issue? On the whole, I would like to think the need for "var" should be very minimal and it would be a lot cleaner to use "val" everywhere.
e
Not really particular problem. In general we are advice to use
listOf
instead of
mutableListOf
. And
val
instead of
var
. But if we need to somehow modify the list, I’m thinking which should we more linear a bit, should it be always 1. stick of
val
and no
var
.. then use
mutableListOf
, or 2. tick to
listOf
and no
mutlableListOf
, then use `var`…. Or 3. there are different situation to trade off one vs the other. From your explanation, it sounds like you are for 1. Correct?
h
Yes! I think using "val" would be less error prone in the long run. If you think about it, when wanting to add things or remove things from a list, you're removing properties or entries within the object. This does not require you to rebind to a different object. When defining classes, you can define them with mutable ("var") properties, but when creating objects from those classes it makes more sense to make the object immutable with "val". This way you wouldn't be able to accidentally reassign to a different object.