Placeholder Syntax in Scala

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.

Example 1:
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:



scala> someNumbers.filter(x=>x>5) 

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.

Example 2:
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.

Example 3:

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.



My Personal Notes arrow_drop_up

A passionate computer engineer, hardworking, poised person who likes to write articles related to coding

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.