让我们定义一个PartialFunction[String, String]
and a PartialFunction[Any, String]
现在,给出定义orElse
def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]): PartialFunction[A1, B1]
我预计无法将两者组合起来,因为
A
→ String
A1
→ Any
因此有界A1 <: A
(i.e. Any <: String
)不成立。
没想到我竟然能把它们组合起来并获得PartialFunction[String, String]
总体上定义String
领域。这是一个例子:
val a: PartialFunction[String, String] = { case "someString" => "some other string" }
// a: PartialFunction[String,String] = <function1>
val b: PartialFunction[Any, String] = { case _ => "default" }
// b: PartialFunction[Any,String] = <function1>
val c = a orElse b
// c: PartialFunction[String,String] = <function1>
c("someString")
// res4: String = some other string
c("foo")
// res5: String = default
c(42)
// error: type mismatch;
// found : Int(42)
// required: String
此外,如果我明确提供orElse
类型参数
a orElse[Any, String] b
// error: type arguments [Any,String] do not conform to method orElse's type parameter bounds [A1 <: String,B1 >: String]
编译器终于显示出一些意义。
是否有我缺少的任何类型系统魔法导致b
成为一个有效的论据orElse
?换句话说,怎么会这样A1
被推断为String
?
如果编译器推断A1
from b
那么它一定是Any
,那么导致的推理链还会在哪里呢?String
start?
Update
使用 REPL 后我注意到orElse
返回交集类型A with A1
当类型不匹配时。例子:
val a: PartialFunction[String, String] = { case "someString" => "some other string" }
// a: PartialFunction[String,String] = <function1>
val b: PartialFunction[Int, Int] = { case 42 => 32 }
// b: PartialFunction[Int,Int] = <function1>
a orElse b
// res0: PartialFunction[String with Int, Any] = <function1>
Since (String with Int) <:< String
即使生成的函数实际上无法使用,这也是可行的。我也怀疑String with Any
统一为Any
, 鉴于
import reflect.runtime.universe._
// import reflect.runtime.universe._
typeOf[String] <:< typeOf[String with Any]
// res1: Boolean = true
typeOf[String with Any] <:< typeOf[String]
// res2: Boolean = true
这就是为什么混合String
and Any
结果变成String
.
话虽如此,幕后到底发生了什么?不匹配的类型是在什么逻辑下统一的?
Update 2
我已将问题简化为更一般的形式:
class Foo[-A] {
def foo[B <: A](f: Foo[B]): Foo[B] = f
}
val a = new Foo[Any]
val b = new Foo[String]
a.foo(b) // Foo[String] Ok, String <:< Any
b.foo(a) // Foo[String] Shouldn't compile! Any <:!< String
b.foo[Any](a) // error: type arguments [Any] do not conform to method foo's type parameter bounds [A <: String]