Scala: value :: is not a member of Int

scala

Solution

Infix operators are interpreted as method calls in Scala. If the infix operators ends with a colon, it's a method call on the right operand with the left operand as its argument. Otherwise it's a method call on the left operand with the right operand as its argument.

In other words, if you do `x + y`, it's the same as `x.+(y)`, i.e. you're calling the method `+` on the object `x`, with `y` as the argument. And if you do `x :: y` it's the same as `y.::(x)`, calling the method `::` on the object `y`.

So in your example you're calling the method `::` on the object `1`, which is an `Int`. However the class `Int` does not have a `::` method, so this does not work and you get an error message telling you that the `::` method does not exist for the `Int` class.

To make `::` work, the right operand needs to be a list (or something else that has a `::` method), so `2 :: 1 :: Nil` would work. However in this case using `List()` seems like the cleaner alternative.

Problem

I recently started using scala and I can't make anything of the error messages. For the following code I get the stated message(using eclipse): ``` def helper: Int => List[Int] = x => x match { case 2 => 2::1 ... } ``` I can fix it by using List(2,1), but souldn't that be the same thing as 2::1? I have similar problems where the List(...) approach would be harder to use, so I really want to know where my thinking mistake is.

Original source