Scala – Placeholder Syntax

  • date 24th May, 2021 |
  • by Prwatech |
  • 0 Comments

The requirement for making everything brief lead Scala to raise something many refer to as the Placeholder sentence structure. Scala permits the utilization of underscore (meant as ‘_’) to be utilized as placeholders for at least one boundaries. we can consider the underscore to something that should be filled in with a worth.

Use case 1:

scala> val somenumbers= List(1,2,3,4,5)
scala>  somenumbers.foreach(println _)

Use case 2:

Suppose we want to check if the number is greater than 5.
scala> val somenumbers= List(3,4,5,8,10,9)
scala> somenumbers.filter(_ > 5)

Use case 3:

Suppose we want to add two numbers.
scala> val f = (_: Int) + (_: Int)
scala> f(15, 10)

Quick Support

image image