Last week I read about Scala’s variable definition. I note an interesting way to define a variable which make me write this post. Scala has two type of variables which can be defined with var and val. These key words shows how to use a variable. For example if we define variable with var ‘var name : String = “Some Name” ‘ that’s mean that we can change the value of the variable. Usage of val ‘val name : String = “Some Name” ‘ is that we define constant value for some reason. If you try to assign a value to variable defined with val you will receive error “reassignment to val”.
What I like in this approach of defining variable is the fact that you can show your desire to not change value for given variable very easy and you don’t need of modifiers which sometimes must be carried with the variable in some languages like C# and C++.