Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the single-right-arrow (→) mean for collections in Scala?

When I use auto-complete on lists on InteliJ, it shows a single-right-arrow, with no documentation on what it means. It looks like →. In an example, it's called as:

val sampleList: List[String] = List("a", "b", "c");
sampleList.→()

I don't know what goes in the parenthesis, I can't use it like a spark map either, so doing s => s shows an error. And on the Scala documentation online, the arrow operator isn't listed.

This is how it shows up on the auto-complete enter image description here

What would be an example usage of this arrow operator?

like image 274
sparkonhdfs Avatar asked Apr 20 '18 20:04

sparkonhdfs


1 Answers

-> isn't defined specifically on collections, it's defined on Any (via the implicit class ArrowAssoc). You can see its definition in Predef.scala.

It is an alternative syntax for creating a Tuple2:

scala> 1 -> 2
res0: (Int, Int) = (1,2)

scala> List().->(2)
res1: (List[Nothing], Int) = (List(),2)

scala> (1 -> 2) == ((1, 2))
res2: Boolean = true
like image 71
Marth Avatar answered Oct 11 '22 09:10

Marth