-3

Here is my discovery. I've found that in Scala I can simply write such expression:

val n: Int = (((2: Int): Int): Int): Int

and it'll 100% valid. But I have no idea why Scala creators did that? What is the point of this semantic? And are there some reasons for that kind of weirdness?

PS

I can assume that such approach is used for type inference to help compiler figure out what is going on. Or maybe there are some other cases?

Finkelson
  • 2,921
  • 4
  • 31
  • 49

1 Answers1

6

Answer

  1. Yes, the syntax is valid.
  2. What you are doing is called "type ascription", telling the compiler what type you expect out of an expression. (see What is the purpose of type ascriptions in Scala? )
  3. Where do you see any problems? AnInt (2 in your case) is an Int (and stays an Int). Even if you repeat that multiple times via ( (previous : ...) : Int ) it still stays an Int.
Community
  • 1
  • 1
Martin Senne
  • 5,939
  • 6
  • 30
  • 47