Why does Scala choose to have the types after the variable names?

Kevin’s got it right. The main observation is that the “type name” syntax works great as long as types are short keywords such as int or float:

int x = 1
float d = 0.0

For the price of one you get two pieces of information: “A new definition starts here”, and “here’s the (result) type of the definition”. But we are way past the area of simple primitive types nowadays. If you write

HashMap<Shape, Pair<String, String>> shapeInfo = makeInfo()

the most important part of what you define (the name) is buried behind the type expression. Compare with

val shapeInfo: HashMap[Shape, (String, String)] = makeInfo()

It says clearly

  • We define a value here, not a variable or method (val)
  • The name of the thing we define is shapeInfo
  • If you care about it, here’s the type (HashMap[…])

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)