rod*_*ty1 7 scala type-constructor
为什么调用foo(1)在我的Scala 2.11.7 repl中工作如下所述?
scala> def foo[F[_], A](fa: F[A]) = null
foo: [F[_], A](fa: F[A])Null
scala> foo(List(1))
res0: Null = null
scala> foo(1)
res1: Null = null
Run Code Online (Sandbox Code Playgroud)
我调用的参数foo(1)不是Type Constructor,那么为什么Scala repl会接受它呢?
foo(1)之所以有效,是因为有一个隐式转换int2Integer为java.lang.Integer
foo(int2Integer(1))
Run Code Online (Sandbox Code Playgroud)
是Integer一个实例Comparable[Integer]
public final class Integer extends Number implements Comparable<Integer>
Run Code Online (Sandbox Code Playgroud)
我们看到的Comparable确实是一个类型构造函数。例如,我们可以像这样重现相同的行为
def foo[F[_], A](fa: F[A]) = 42
trait Bar // not a type constructor
implicit def barToComparable(b: Bar): Comparable[Int] = (o: Int) => -1
val bar = new Bar {}
foo(bar) // OK due to implicit conversion barToComparable
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93 次 |
| 最近记录: |