Question

What does the underscore mean in below snipped. This is fragment of scalaz7 library:

trait Apply[F[_]] extends Functor[F] { self =>
  //...
  def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B]
  //...
  def apF[A, B](f: => F[A => B]): F[A] => F[B] = ap(_)(f)   // <----HERE
  //...
}

What are the general rules of using that?

Was it helpful?

Solution

In Scala, the underscore in general is a wildcard character. Here specifically, it is a shorthand for a parameter name. So the lambda expression ap(_)(f) is equivalent to x => ap(x)(f).

You can use _ as shorthand for the parameter(s) of an anonymous function if each parameter is used only once, and they are used in the order of their declaration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top