Exactly because of this reason.
value :: is not a member of Int
In Scala, the operators are actually functions on objects. In this case, ::
is a function on Nil
object, which is actually an Empty list object.
scala> Nil
res0: scala.collection.immutable.Nil.type = List()
When you do 1::2
, Scala looks for the function named ::
on 2
and it doesn't find that. That is why it fails with that error.
Note: In Scala, if the last character of the operator is not colon, then the operator is invoked on the first operand. For example, 1 + 2
is basically 1.+(2)
. But, if the last character is colon, the the operator is invoked on the right hand side operand. So in this case, 1 :: Nil
is actually Nil.::(1)
. Since, the ::
returns another list object, you can chain it, like this 1 :: 2 :: Nil
is actually, Nil.::(2).::(1)
.