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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With