20

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
  • 295,120
  • 86
  • 501
  • 681
tom
  • 14,273
  • 19
  • 65
  • 124

4 Answers4

28

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
  • 142,018
  • 20
  • 234
  • 287
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
  • 40,677
  • 6
  • 91
  • 113
1

Seems that head returns a Tuple2

Peter Schmitz
  • 5,824
  • 4
  • 26
  • 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,871
  • 2
  • 19
  • 22