Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala @ operator

What does Scala's @ operator do?

For example, in the blog post Formal Language Processing in Scala, Part 2 there is a something like this

case x @ Some(Nil) => x
like image 585
lowercase Avatar asked Mar 01 '10 20:03

lowercase


2 Answers

It enables one to bind a matched pattern to a variable. Consider the following, for instance:

val o: Option[Int] = Some(2)

You can easily extract the content:

o match {
  case Some(x) => println(x)
  case None =>
}

But what if you wanted not the content of Some, but the option itself? That would be accomplished with this:

o match {
  case x @ Some(_) => println(x)
  case None =>
}

Note that @ can be used at any level, not just at the top level of the matching.

like image 190
Daniel C. Sobral Avatar answered Sep 28 '22 04:09

Daniel C. Sobral


@ can be used to bind a name to a successfully matched pattern, or subpattern. Patterns can be used in pattern matching, the left hand side of the <- in for comprehensions, and in destructuring assigments.

scala> val d@(c@Some(a), Some(b)) = (Some(1), Some(2))
d: (Some[Int], Some[Int]) = (Some(1),Some(2))
c: Some[Int] = Some(1)
a: Int = 1
b: Int = 2

scala> (Some(1), Some(2)) match { case d@(c@Some(a), Some(b)) => println(a, b, c, d) }
(1,2,Some(1),(Some(1),Some(2)))

scala> for (x@Some(y) <- Seq(None, Some(1))) println(x, y)
(Some(1),1)

scala> val List(x, xs @ _*) = List(1, 2, 3) 
x: Int = 1
xs: Seq[Int] = List(2, 3)
like image 37
retronym Avatar answered Sep 28 '22 04:09

retronym