0

I'm reading a tutorial for demonstration about lifting method in Scala. Here is the code for basic class:

case object None extends Optional[Nothing]

case class Presenter[+A](get: A) extends Optional[A]

trait Optional[+A] {
  def map[B](f: A => B): Optional[B] = this match {
    case None => None
    case Presenter(x) => Presenter(f(x))
  }
}

Here is the lift method:

  def lift[A, B](f: A => B): Optional[A] => Optional[B] = _ map f

My question is: what does underscore ("_") represent in above function.

thanks

Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107
  • lift returns a new function from Optional[A] to Optional[B]. So Optional[A] is the input argument of this new function. So in this case, the _ represents the argument Optional[A] on which the 'map' function is called with the 'f' argument of the 'lift' function. – Joost den Boer Feb 27 '17 at 08:51

1 Answers1

2

_ map f is equal to (a: Optional[A]) => a.map(f), _ is wildcard in Scala. for your example, it means for parameter with type Optional[A] and apply to map f

chengpohi
  • 14,064
  • 1
  • 24
  • 42