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?
Join them; it only takes a minute:
|
|
||||
|
|
|
As so many others have said, the object assigned to a
So, even though we can't change the object assigned to 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 That's one reason, but there is another reason for it. When you use
Simply put, using We can, then, go the other direction. If For example, take an immutable 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
This code is fast and easy to understand. Its main drawback is that the queue that is passed is modified by Now, let's covert it to an
Because I can't reuse some variable to keep track of my Note, however, that I can rewrite that code to use an
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 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. |
|||||||||||||||||
|
|
|
|||||||||||||||||||||
|
|
In simple terms: var = variable val = variable + final |
|||
|
|
|
The difference is that a
Whereas:
And hence:
If you are building a data structure and all of its fields are |
|||||||||||||
|
|
|
|||||||||
|
|
Thinking in terms of C++,
is analogous to constant pointer to non-constant data
while
is analogous to non-constant pointer to non-constant data
Favoring |
|||||
|
|
"val means immutable and var means mutable." To paraphrase, "val means value and var means variable". A distinction that happens to be extremely important in computing (because those two concepts define the very essence of what programming is all about), and that OO has managed to blur almost completely, because in OO, the only axiom is that "everything is an object". And that as a consequence, lots of programmers these days tend not to understand/appreciate/recognize, because they have been brainwashed into "thinking the OO way" exclusively. Often leading to variable/mutable objects being used like everywhere, when value/immutable objects might/would often have been better. |
|||||
|
you can think |
|||
|
|
|
A val is similar to a final variable in Java. Once initialized, a val can never be reassigned. A var, by contrast, is similar to a non-final variable in Java. A var can be reassigned throughout its lifetime. |
|||
|
|
|
It's as simple as it name.
|
|||
|
|