147

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lowercase
  • 2,194
  • 3
  • 17
  • 15

5 Answers5

206

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.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • 7
    Where in the documentation would I find that answer? I have a feeling there's other good stuff buried there as well. :) – Jim Barrows Mar 02 '10 at 22:47
  • 1
    @Jim Scala Reference, 8.1. 8.12, specifically, though I don't know where the "as usual" there came from -- and 8.12 only speak of regular expression pattern (`_*`). But maybe this has been clarified on a newer version of the spec. – Daniel C. Sobral Mar 02 '10 at 22:58
  • 17
    I would add that you would probably not use `@` with `Some(_)`, but rather if you wanted to match on the contents of the `Some`, but still refer to the Some itself, e.g. `case x @ Some(7) => println(x)`. As I interpret it `case x @ Some(_)` is just a more verbose version of `case x: Some`. – Theo Mar 03 '10 at 13:22
  • 5
    This is also covered under "Variable binding" in Section 15.2 of "Programming in Scala - 2nd Edition" and used again in section 26.3 (the chapter on extractors). – Shaun the Sheep Sep 18 '11 at 20:46
  • 1
    @Theo `case x: Some` doesn't work on its own. You have to use `case x: Some[_]`, which is no less verbose – Luigi Plinge Oct 25 '11 at 19:47
  • Can we use @ to return the result of applying some function on the matched item. For example: Match `a` of type `Any` and if it is of type `Long` then return `a.toString`. This can be done naively as `def foo(a:Any) = a match {case l:Long => l.toString}`. But I want the Left side of `=>` to return a `String` rather than `Long`, but only if `a` is of type `Long`. – Jus12 Oct 17 '14 at 07:32
  • @Jus12 Sorry, I don't understand what you are asking. Please post a question instead of asking with a comment. – Daniel C. Sobral Oct 18 '14 at 01:16
  • @DanielC.Sobral `def foo(a:Any) = a match { case l:Long => val x = bar(l); // do something with x }`. In this case, I am interested in `bar(l)`, not `l`. Can we do the match so that the RHS of `=>` has `bar(l)` to work with directly rather than `l`; i.e., the `bar(l)` is computed on LHS of `=>`. Something like `case l:Long (x@bar(l)) =>` (this doesn't work). – Jus12 Oct 18 '14 at 04:56
84

@ 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)
retronym
  • 54,768
  • 12
  • 155
  • 168
  • 1
    Underrated answer. In my opinion, far more complete than the original for the explicit mention and examples of subpatterns. It is a bit cryptic at first, but very comprehensive when you take some time with it - even 12 years later. – Lucas Lima Apr 08 '22 at 18:23
11

Allows you to match the top-level of a pattern. Example:

case x @ "three" => assert(x.equals("three"))
case x @ Some("three") => assert(x.get.equals("three")))
case x @ List("one", "two", "three") => for (element <- x) { println(element) }
Mitch Blevins
  • 13,186
  • 3
  • 44
  • 32
11

When pattern matching variable @ pattern binds variable to the value matched by pattern if the pattern matches. In this case that means that the value of x will be Some(Nil) in that case-clause.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
2

It sets the value of x to the pattern which matches. In your example, x would therefore be Some(Nil) (as you could determine from a call to println)

oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449