Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason for having "val capacity : Int" instead of "val Int Capacity" in Scala

I am reading Scala and I am wondering ...
Why

val capacity : Int 

instead of

val Int capacity.

Any reason why this choice was made. If not, it does not seem to me like a good choice to move away from the Java way of declaring it. Would have made the transition from Java to Scala easier (not by much, but little bit)

like image 606
user855 Avatar asked Dec 12 '09 00:12

user855


2 Answers

Because the majority of the time you can leave off the Int part. Scala has a much neater system of type inference than Java does.

like image 58
Wysawyg Avatar answered Oct 23 '22 12:10

Wysawyg


I think I read a statement by Martin Odersky himself somewhere saying that this decision was also made in order to improve readability. This is certainly the case, e.g. compare

val Double number = ...
val Array[(Seq[String], Map[Seq[String], Double])] something = ...
val String pattern = ...

with

val number : Double = ...
val something : Array[(Seq[String], Map[Seq[String], Double])] = ...
val pattern : String = ...

Most of the time you need to find names of references/mathods fast (visually), not types.

like image 32
Raphael Avatar answered Oct 23 '22 12:10

Raphael