Search code examples
scalacons

Why does cons function called explicity works over Int in Scala?


You can create a new List in Scala using:

1 :: 2 :: Nil

In my understanding this can rewritten to:

Nil.::(2.::(1))

Mainly because :: fixty but if I write:

Nil :: 1 :: 2

I get "value :: is not a member of Int" what is totally expected because in scaladoc Int does not have ::, but I can't understand why if I translate that to:

1.::(2.::(Nil))

It works getting as output:

List(1.0, 2.0)

It looks like scalac auto casts the 1 and 2 to a type different than Int. Is that correct? If it is, why does it happen and which is this strange type?


Solution

  • This is funny.

    Your expression

    1.::(2.::(Nil))
    

    is being parsed by the compiler as

    1. :: (2. :: (Nil))
    

    which, since :: is right-associative, is the same as

    1. :: 2. :: Nil
    

    which, since 1. is a valid way of writing a Double, is the same as

    1.0 :: 2.0 :: Nil
    

    which is a legal expression for constructing the List[Double]

    List(1.0, 2.0)