Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala's mutable and immutable set when to use val and var

Tags:

scala

I'm reading the Programming in Scala book by the Scala Creator and I'm a bit confuse on the example of Set.

Here it is for Immutable Set:

var jetSet = Set("Boeing", "Airbus")
jetSet += "Lear"
println(jetSet.contains("Cessna"))

What's the point of this?

The set is immutable but the variable jetSet is mutable. 1) So every time I add to the set with += it creates a new set? So the variable point to a new set in memory?

2) Shouldn't it be: val jetSet = set("cow","sheep","duck") ? Why does it have to be a var? Is there a reason to use var for a immutable set?

like image 948
mythicalprogrammer Avatar asked Jun 27 '11 05:06

mythicalprogrammer


People also ask

What is difference between VAR and val?

The difference between val and var is that val makes a variable immutable — like final in Java — and var makes a variable mutable. Because val fields can't vary, some people refer to them as values rather than variables.

Why do we use val in Java?

You can use val as the type of a local variable declaration instead of actually writing the type. When you do this, the type will be inferred from the initializer expression. The local variable will also be made final. This feature works on local variables and on foreach loops only, not on fields.

What is val and VAR in Kotlin?

A variable declared with `var` is mutable. A variable declared with `val` is immutable. A variable declared with `const` is immutable. Values are assigned at run time. Values are assigned at run time.

Why is variable immutable?

Such variables cannot change their value or state. Therefore, once we assign them the value during declaration we cannot make changes in the future if a need arises.


2 Answers

The advantage of immutable data structures, in this case Set, is that they are persistent. For example:

var jetSet1 = Set("Boeing", "Airbus")
val jetSet2 = jetSet1 // ... imagine jetSet2 is somewhere else in the program
jetSet1 += "Lear"
assert(!jetSet2.contains("Lear"))

Immutability of these Set objects makes it easier to reason about the program because updates to the jetSet1 variable don't have side effects in other parts of the code (in this case, wherever jetSet2 is used). Although it's not clear from this example, there are occasions when it's convenient to store immutable values in mutable var references; most often, the var will have a limited scope (e.g., local to a function).

Immutable data structures often have clever implementations that are quite efficient. Unfortunately, the Scala collection API is not well documented regarding performance, but I would expect most operations to be roughly O(log N) time. For example, given a large immutable Set s, one should be able to efficiently construct s + x, a new Set with an extra element. Of course, immutability guarantees that s is also preserved. Under the hood, s and s+x will be stored using some kind of tree data-structures with shared components.

The title of your question suggest you are also looking for advice about using val or var. The rule of thumb is to use val whenever you can conveniently. If a var is necessary, then try to limit the variable's scope as much as possible.

like image 119
Kipton Barros Avatar answered Dec 20 '22 14:12

Kipton Barros


var allows you to reassign to a variable, think of it as a normal variable declaration in Java. But in the above case, even though you are reassigning to the same variable, it's always a different set since you are using an immutable one.

The point of the example was to show that even if you try "modifying" an immutable collection, the modifications create a new collection instead of touching the original one. If the author would have used a val (along the lines of final in Java), he would have to introduce a variable in scope to hold the new immutable set. I think var was probably used to keep the example simple.

like image 35
Sanjay T. Sharma Avatar answered Dec 20 '22 14:12

Sanjay T. Sharma