Scala has values(val) and variables(var).
First impression is bit confusing as they looks the same but in practice ... they are ALMOST the same.
Values are immutable.It means you can NOT change them after bind them
Variable are mutable.It means you can change them after bind them.
However! object even if is assigned to value (immutable) ,object can modify its internal state!
If you come to Scala from Java world,then you can memorize this as values is a like variable with final keyword.(final means more less: "entity that cannot later be changed after assign").
Value example:
val multiply = 4 * 6
(! if you try to assing anything to multiply after that you will see error: "reassignment to val".)
Variable example:
var name = "cheese"
name = "marius"
I am not sure ,but i think, it had to do something with mixing 2 paradigms of Functional Programming and Object Oriented Programming,so split into val and var can serve both paradigms in their way.
Sources:
- http://twitter.github.io/scala_school/basics.html
- http://twitter.github.io/effectivescala/
- http://ofps.oreilly.com/titles/9780596155957/
- http://stackoverflow.com/questions/1791408/what-is-the-difference-between-a-var-and-val-definition-in-scala
No comments:
Post a Comment