18

What does the _2 mean in the following code? Where can I find the official documentation for this?

.. 
@if(errors) {
    <p class="error">
        @errors.head._2
    </p>
}
...
Daniel C. Sobral
  • 290,652
  • 84
  • 491
  • 673
tom
  • 13,733
  • 19
  • 64
  • 120

4 Answers4

26

The ._2 selects the second element in a tuple e.g.

val t = (1,2)
t._2

so @errors in your sample appears to be a list of tuples. You can find the documentation here for Tuple2, and there are Tuple3, Tuple4 etc. classes for tuples of size 3, 4 etc. The scala package documentation shows the available Tuple types which go up to size 22.

Lee
  • 138,123
  • 20
  • 222
  • 279
4

In this instance, I believe _2 is just a field name, representing the 2nd field of a Tuple2 object.

The underscore is sometimes a bit more magical, however. It's used as a wildcard in import statements, as a non-assigning placeholder in assignments that need a value for syntax but shouldn't actually do any, and as a variable that should be there but whose value doesn't matter in pattern matching.

Don Roby
  • 39,978
  • 6
  • 86
  • 108
1

Seems that head returns a Tuple2

Peter Schmitz
  • 5,764
  • 4
  • 25
  • 48
-5

It's for pattern matching, you can find documentation about it here

edit: I believe its main purpose is to match anything, for example if you do "import http._" it will import everything from that library.

LainIwakura
  • 2,813
  • 2
  • 17
  • 22