Placeholder Syntax in Scala
Last Updated :
11 Sep, 2019
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.
Similar Reads
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. H
3 min read
Scala Long <=(x: Short) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The <(x: Short) method is utilized to return true if this value is less than or equal to x, false otherwise. Method Definition - def <=(x: Short): Boolean Returns - Returns true if this value is less
1 min read
Scala Long <(x: Short) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The <(x: Short) method is utilized to return true if this value is less than x, false otherwise. Method Definition - def <(x: Short): Boolean Returns - Returns true if this value is less than x, false
1 min read
Scala Long >=(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Int) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Int): Boolean Returns - Returns true if this value is great
1 min read
Scala Long <=(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The <(x: Int) method is utilized to return true if this value is less than or equal to x, false otherwise. Method Definition - def <=(x: Int): Boolean Returns - Returns true if this value is less than
1 min read
Scala Long >(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >(x: Int) method is utilized to return true if this value is greater than x, false otherwise. Method Definition - def >(x: Int): Boolean Returns - Returns true if this value is greater than x, fal
1 min read
Scala Long <(x: Int) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The <(x: Int) method is utilized to return true if this value is less than x, false otherwise. Method Definition - def <(x: Int): Boolean Returns - Returns true if this value is less than x, false oth
1 min read
Packages In Scala
Package in Scala is a mechanism to encapsulate a group of classes, sub packages, traits and package objects. It basically provides namespace to put our code in a different files and directories. Packages is a easy way to maintain our code which prevents naming conflicts of members of different packa
4 min read
Scala Double <=(x: Int) method
In Scala, Double is a 64-bit floating point number, which is equivalent to Javaâs double primitive type. The <=(x: Int) method is utilized to return true if this value is less than or equal to x, false otherwise. Method Definition - def <=(x: Int): Boolean Returns - Returns true if this value
1 min read
Scala Long >=(x: Short) method
In Scala, Long is a 64-bit signed integer, which is equivalent to Java's long primitive type. The >=(x: Short) method is utilized to return true if this value is greater than or equal to x, false otherwise. Method Definition - def >=(x: Short): Boolean Returns - Returns true if this value is g
1 min read