문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top