61

I'm trying to pick up some scala. Reading through examples I came across this impossible-to-google nugget:

case 3 => l ::: List(3)

What does the triple colon accomplish?

providence
  • 29,135
  • 13
  • 46
  • 62
  • 2
    See also http://stackoverflow.com/questions/6566502/whats-the-difference-between-and-in-scala/6566523 – Debilski Oct 01 '11 at 15:39

2 Answers2

66

Concatenates two lists - javadoc

gkamal
  • 20,777
  • 4
  • 60
  • 57
53

To add to gkamal's answer, it's important to understand that methods whose names end in a colon are right-associative. So writing l ::: List(3) is the same as writing List(3).:::(l). In this case it doesn't matter since both operands are lists, but in general you'll need this knowledge to find such methods in the scaladocs.

It also helps to know that the scaladocs have a comprehensive index of all methods (and classes, etc) with symbolic names. You can reach it by clicking on the # in the upper-left corner.

Community
  • 1
  • 1
Aaron Novstrup
  • 20,967
  • 7
  • 70
  • 108
  • `#` works on nightly (2.10.0.r...) http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/index.html#index.index-_ – Mark Jayxcela Oct 03 '11 at 19:01
  • `it's important to understand that methods whose names end in a colon are right-associative` but `:::` also starts with a colon, and thus the case can be made that it is left associative. How is it then decided that it should be right associative? – dade Jun 17 '18 at 09:13
  • @dade I think the rule is pretty simple: methods are left-associative by default, but a method whose name _ends_ in a colon is right-associative. Whether those semantics are intuitive or not is a separate issue. :) There may be other cases where symbols in the method name change the default associativity (I'd have to refer to the language spec), but _starting_ with a colon is not one of them. – Aaron Novstrup Jun 22 '18 at 18:39