Type aliases might typically be used like this:
type Point = (Double, Double)
but I was playing with the scala repl and you could do this as well:
type Two = 2
with this alias you can define the function:
scala> def three(x: Two) = x + 1
def three(x: Two): Int
scala> three(1)
-- Error:
1 |three(1)
| ^
| Found: (1 : Int)
| Required: Two
scala> three(2)
val res0: Int = 3
As you can see Two really does seem to represent the singleton type of 2. It only seems to work with value literals, not variables:
scala> val x = 2
val x: Int = 2
scala> type Two = x
-- Error:
1 |type Two = x
| ^
| Not found: type x
Also it seems to work with some function values but not others:
scala> type AlwaysThree = () => 3
// defined alias type AlwaysThree = () => 3
scala> type AlwaysThree = () => 2 + 1
-- Error:
1 |type AlwaysThree = () => 2 + 1
| ^
| Not found: type +
I'm wondering why scala allows this. Is there a reason this little thing is here?