The need for making everything concise lead Scala to bring up something called as the Placeholder syntax. 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.
Note: Internally, the underscore is expanded into a literal for a function that takes input as 1 parameter and then checks the condition which is mentioned. The same rule applies for more than 1 parameter. Multiple underscores means multiple parameters and not the use of same parameter repeatedly. Hence, this syntax is used when you want to take 1 or more parameters only once. This syntax doesn’t support re-usability.
Let’s understand placeholder syntax with the help of an example.
Suppose we want to check if the number is greater than 5.
scala> someNumbers.filter(_ > 5)
In the above example, the condition is expanded internally as:
The variable x has been replaced by an underscore to make the function more concise.
The placeholder syntax can also be used to denote more than single parameter.
Suppose we want to add two numbers. If we understand the above example, we would think the normal syntax for the addition of two numbers should be:
scala> val f = _ + _
When this is passed to the compiler, it gives an error since you haven’t mentioned the ‘type’ of numbers that the compiler should take as an input.
val f = _ + _ ˆ error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
we should mention the type of numbers that we expect from the function. Type can be passed to the function with the help of colon(‘:’). Note that internally,
f = _ + _
is expanded as
((x$1, x$2) => x$1.$plus(x$2)).
Now, the correct placeholder syntax for addition of two numbers using a function is:
scala> val f = (_: Int) + (_: Int) f: (Int, Int) => Int = <function> //Input scala> f(15, 10) //Output res: Int = 25
Hence, while passing more than one parameter, make sure that we specify the type of the variable that we are passing . So that the compiler understands what kind of input it is expected to take.
scala> r.map((x,y) => x + y / x min y)
In above example the parameters used more than once. we need four underscore in that case. according to the rule number of parameter and number of underscore must be same so we can not use placeholder syntax.
- CSS | ::placeholder Selector
- How to make a placeholder for a 'select' box?
- How to set placeholder value for input type date in HTML 5 ?
- Change an HTML5 input placeholder color with CSS
- CSS | Syntax and Selectors
- jQuery | Syntax
- Ruby Basic Syntax
- Scala | Either
- Set in Scala | Set-2
- Set in Scala | Set-1
- Scala Map
- Remove key value from Map in Scala
- Scala Set min() method with example
- Scala Set map() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.