Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of _2 sign in scala language

Tags:

scala

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>
}
...
like image 453
tom Avatar asked Jun 19 '11 19:06

tom


People also ask

What does _ _1 mean in Scala?

_1 is a method name. Specifically tuples have a method named _1 , which returns the first element of the tuple.

What does this symbol => Do in Scala?

=> 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).

What does an underscore mean in Scala?

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.

What is the use of tuples in Scala?

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.


1 Answers

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.

like image 128
Lee Avatar answered Oct 03 '22 23:10

Lee