3 ms·
Why was Scala designed to have var/val/def for variable annotations?
Can someone explain to me why Scala was designed to have var/val/def for variable annotations? All of the following are valid ways to declare a variable:
def a = 1
val b = 1
var c = 1
I know the difference between them. But why was it designed that way? Is it really necessary to have this type of confusion?
http://stackoverflow.com/questions/4437373/use-of-def-val-and-var-in-scala
- dragonwriter 13y agoPresumably 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.