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>
}
...
_1 is a method name. Specifically tuples have a method named _1 , which returns the first element of the tuple.
=> is the "function arrow". It is used both in function type signatures as well as anonymous function terms. () => Unit is a shorthand for Function0[Unit] , which is the type of functions which take no arguments and return nothing useful (like void in other languages).
Scala allows the use of underscores (denoted as '_') to be used as placeholders for one or more parameters. we can consider the underscore to something that needs to be filled in with a value. However, each parameter must appear only one time within the function literal.
In Scala, a tuple is a value that contains a fixed number of elements, each with its own type. Tuples are immutable. Tuples are especially handy for returning multiple values from a method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With