

Why was Scala designed to have var/val/def for variable annotations? - neuhaus

Can someone explain to me why Scala was designed to have var&#x2F;val&#x2F;def for variable annotations? All of the following are valid ways to declare a variable:<p>def a = 1<p>val b = 1<p>var c = 1<p>I know the difference between them. But why was it designed that way? Is it really necessary to have this type of confusion?<p>http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;4437373&#x2F;use-of-def-val-and-var-in-scala
======
dragonwriter
Presumably it was designed that way because scala is designed to be a
functional programming language, motivating a concise syntax for function
definition (def), that favors immutable variables (val), but supports mutable
state where it simplifies expression and to ease transition of, e.g., Java
programmers (var).

> Is it really necessary to have this type of confusion?

For Scala's goals, all of those serve distinct and important purposes. I mean,
sure, you could have less concise forms that made things more explicit to
people who've never used scala, like:

    
    
      declare a as method returning 1
      declare b as fixed value of 1
      declare c as mutable variable initialized with value of 1
    

But, I mean, think of the keystrokes!

It is something to learn, but it is a fairly straightforward distinction, and
it lets Scala express a lot cleanly and concisely.

