I’ve been diving into scala, and am absolutely loving it so far. I’m getting around to pattern matching and case classes, and the following has me somewhat stumped. I know what it does, but I want to understand exactly what is happening
var list = List(2,3,4)
1::list
If I am correct in my understanding so far. Then the :: represents a case object. If so my question is:
how am I “left applying” it to the 1? instead of :: being a method of 1. Basically can someone pull this statement 1::list apart somewhat, showing what is really happening (ie. what methods are being called on what object)
Thanks
It’s annoying that some of the most visible and awesome features of Scala have so much complexity right beneath the surface. So, consider this simple line:
Each of the three places where
::appears refers to a different::, and a different mechanism in Scala. Let’s go through each of them, in order.head :: tailWhat is happening here is pattern matching, just like one sees with
casestatements. Pattern matching can appear onvalassignments, on the left side of<-inforcomprehensions, and oncasestatements.So, how does this particular pattern matching happens? Well, whenever the pattern is in the format
a b c, Scala translates this intob(a, c), which is then translated into calls tounapplyorunapplySeqon the objectb.So,
::inval (head :: tail)refers to the object::(defined through acase class).: ::[Int]This is a type declaration, so
::[Int]is a type.::itself is a class, and a type constructor as well (because it constructs types given a type parameter —::[Int]is one type,::[String]is another type, etc). It is also a subclass ofList, which has only two subclasses:::and the singleton class ofNil.This declaration is superfluous, and, generally speaking, one hardly ever uses
::as a type or class. I show it here mostly for completeness.1 :: NilHere,
::is a method. It is a method ofList, so, sinceNilis aListand1is not, it must belong toNil(or be available through implicit conversion).The mechanism of note here is that methods ending with
:, when used in infix operator notation, bind to the right instead of to the left. Or, in other words,a :: bis equivalent tob.::(a).This mechanism is rarely used and, I suspect, made mostly to make traditional fp list algorithms more familiar to programmers used to fp. It is used in a few other places on Scala standard library and out of it.
On Scala 2.8, for instance, there’s now
+:, which serves the same purpose of::, but is defined for allSeq. It is mirrored by:+, which appends elements, and whose:serves no purpose other than disambiguate it from+, which is overloaded to concatenate strings.