What is the difference between a var and val definition in Scala and why does the language need both? Why would you choose a val over a var and vice versa?
What is the difference between a var and val definition in Scala and why
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As so many others have said, the object assigned to a
valcannot be replaced, and the object assigned to avarcan. However, said object can have its internal state modified. For example:So, even though we can’t change the object assigned to
x, we could change the state of that object. At the root of it, however, there was avar.Now, immutability is a good thing for many reasons. First, if an object doesn’t change internal state, you don’t have to worry if some other part of your code is changing it. For example:
This becomes particularly important with multithreaded systems. In a multithreaded system, the following can happen:
If you use
valexclusively, and only use immutable data structures (that is, avoid arrays, everything inscala.collection.mutable, etc.), you can rest assured this won’t happen. That is, unless there’s some code, perhaps even a framework, doing reflection tricks — reflection can change “immutable” values, unfortunately.That’s one reason, but there is another reason for it. When you use
var, you can be tempted into reusing the samevarfor multiple purposes. This has some problems:Simply put, using
valis safer and leads to more readable code.We can, then, go the other direction. If
valis that better, why havevarat all? Well, some languages did take that route, but there are situations in which mutability improves performance, a lot.For example, take an immutable
Queue. When you eitherenqueueordequeuethings in it, you get a newQueueobject. How then, would you go about processing all items in it?I’ll go through that with an example. Let’s say you have a queue of digits, and you want to compose a number out of them. For example, if I have a queue with 2, 1, 3, in that order, I want to get back the number 213. Let’s first solve it with a
mutable.Queue:This code is fast and easy to understand. Its main drawback is that the queue that is passed is modified by
toNum, so you have to make a copy of it beforehand. That’s the kind of object management that immutability makes you free from.Now, let’s covert it to an
immutable.Queue:Because I can’t reuse some variable to keep track of my
num, like in the previous example, I need to resort to recursion. In this case, it is a tail-recursion, which has pretty good performance. But that is not always the case: sometimes there is just no good (readable, simple) tail recursion solution.Note, however, that I can rewrite that code to use an
immutable.Queueand avarat the same time! For example:This code is still efficient, does not require recursion, and you don’t need to worry whether you have to make a copy of your queue or not before calling
toNum. Naturally, I avoided reusing variables for other purposes, and no code outside this function sees them, so I don’t need to worry about their values changing from one line to the next — except when I explicitly do so.Scala opted to let the programmer do that, if the programmer deemed it to be the best solution. Other languages have chosen to make such code difficult. The price Scala (and any language with widespread mutability) pays is that the compiler doesn’t have as much leeway in optimizing the code as it could otherwise. Java’s answer to that is optimizing the code based on the run-time profile. We could go on and on about pros and cons to each side.
Personally, I think Scala strikes the right balance, for now. It is not perfect, by far. I think both Clojure and Haskell have very interesting notions not adopted by Scala, but Scala has its own strengths as well. We’ll see what comes up on the future.