Scala's '::' operator, how does it work?

Related searches

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


Now, nothing strange here. The following is strange to me. The operator :: is a function on a list, right? With any function with one 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))

I can go on and use it, but what is the explanation?

From the Spec:

6.12.3 InfixOperations An infix operator can be an arbitrary identifier. Infix operators have precedence and associativity defined as follows.


The associativity of an operator is determined by the operator’s last character. Operators ending in a colon ‘:’ are right-associative. All other operators are left- associative.

You can always see how these rules are applied in Scala by printing the program after it has been through the 'typer' phase of the compiler:

scala -Xprint:typer -e "1 :: Nil"

val r: List[Int] = {
  <synthetic> val x$1: Int = 1;

Basics | Tour of Scala, First Steps Getting Started. Install Scala on your computer and start writing some Scala code! Tour of Scala. The Scala Programming Language Scala combines object-oriented and functional programming in one concise, high-level language. Scala's static types help avoid bugs in complex applications, and its JVM and JavaScript runtimes let you build high-performance systems with easy access to huge ecosystems of libraries.

It ends with a :. And that is the sign, that this function is defined in the class to the right (in List class here).

So, it's List(Foo(2)).::(Foo(40)), not Foo(40).::(List(Foo(2))) in your example.

Scala Documentation, Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. Scala (/ ˈ s k ɑː l ɑː / SKAH-lah) is a general-purpose programming language providing support for both object-oriented programming and functional programming. The language has a strong static type system. Designed to be concise, many of Scala's design decisions are aimed to address criticisms of Java.

One aspect missing in the answers given is that to support :: in pattern matching expressions:

List(1,2) match {
  case x :: xs => println(x + " " + xs)
  case _ => println("")

A class :: is defined :

final case class ::[B](private var hd: B, private[scala] var tl: List[B]) 

so case ::(x,xs) would produce the same result. The expression case x :: xs works because the default extractor :: is defined for the case class and it can be used infix.

Introduction | Tour of Scala, At Scala, we focus on building strategic partnerships with the world’s leading brands to apply a wide array of technology — including digital signs, mobile sensors, audience intelligence, virtual reality and computer vision technology — in the physical space.

The class Foo I just defined does not have the :: operator, so how is the following possible:

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

If the method name ends with a colon (:) the method is invoked on the right operand, which is the case here. If the method name doesn't end with colon, the method is invoked on the left operand. For example, a + b, + is invoked on a.

So, in your example, :: is a method on its right operand, which is a List.

Scala (programming language), Compared to other programming languages, installing Scala is a bit unusual. Scala is unusual because it is usually installed for each of your Scala projects rather than being installed system-wide. Both of the above options manage (via sbt) a specific Scala version per Scala project you create.

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. Scala has been created by Martin Odersky and he released the first version in 2003. Scala smoothly integrates the features of object-oriented and functional languages.

SCALA. Scala Headwear is a lifestyle brand inspired and motivated by the era when a hat was a staple in every person’s wardrobe. Since its inception, Scala has been at the forefront of making the long time tradition of hat wearing relevant for the modern times. Uncompromised style and function, the fashionable yet practical line of accessories and hats combines current trends with thoughtful design and quality craftsmanship.

Scala is a producer of multimedia software. It was founded in 1987 as a Norwegian company called Digital Visjon. It is headquartered near Philadelphia, Pennsylvania, USA, and has subsidiaries in Europe and Asia.

  • It also applies for passing type parameters to type constructors. Let say you have a case class ::[H, T](head : H, tail : T); and class SomeType[A]; then you could do both new SomeType[::[String, Int]]("a", 3) and new SomeType[H :: T]("a", 3)
  • In other words, a ::::: b (just to be silly) is the same as b.:::::(a). Additionally, methods whose names end in colon and used in infix style associate to the right, rather than to the left, so a :: b :: c is the same as c.::(b.::(a)))