Given some simple class Container with a type parameter T
case class Container[T](value:T)
Now defining a method
def test[A,B](b:Container[A=>B]) = {
}
What does this [A=>B] mean ? Is it a bound or some kinde of a function type ?
It is a function. It means that T is a function that takes type A as an input and returns a value of type B.
For example,
val c = Container[String=>Int]( s => s.length() )
c.value("abc") // returns 3, and is the equivalent to c.value.apply("abc")
// which in turn calls the function s => s.length()
// and so returns "abc".length()
The type [A=>B]
is a function that takes inputs of type A
and produces outputs of type B
.
In the context of a constructor, it means you are passing in a function as a parameter to the constructor.
A side note, for instance a function value
val fv = (v: Int) => v + v
creates a function object instance at runtime and extends Function1
trait, which includes an apply
method, as aforementioned. Equivalently,
val fv = new Function1[Int, Int] {
def apply(v: Int) = v + v
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.