Scala :: operator, how it works?

Posted by Felix on Stack Overflow See other posts from Stack Overflow or by Felix
Published on 2010-05-13T13:52:52Z Indexed on 2010/05/13 14:04 UTC
Read the original article Hit count: 205

Filed under:
|
|

Hello Guys,

in Scala, I can make a caseclass case class Foo(x:Int) and then put it in a list like so:

List(Foo(42))

Now, nothing strange here. The following is strange to me. The operator :: is a function on a list, right? With any function with 1 argument in Scala, I can call it with infix notation. An example is 1 + 2 is a function (+) on the object Int. The class Foo I just defined does not have the :: operator, so how is the following possible:

Foo(40) :: List(Foo(2))

?

In scala 2.8 rc1, I get the following output from the interactive prompt:

scala> case class Foo(x:Int)
defined class Foo

scala> Foo(40) :: List(Foo(2))
res2: List[Foo] = List(Foo(40), Foo(2))

scala> 

I can go on and use it, but if someone can explain it I will be glad :)

© Stack Overflow or respective owner

Related posts about scala

Related posts about list