PartialFunction orElse在其类型边界上是否比它应该更宽松?

Gab*_*lla 5 types scala partialfunction

让我们定义一个PartialFunction[String, String]和一个PartialFunction[Any, String]

现在,给定定义 orElse

def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]): PartialFunction[A1, B1] 
Run Code Online (Sandbox Code Playgroud)

我希望不能把这两者组合起来,因为

A→交通String
A1→交通Any

因此,约束A1 <: A(即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
Run Code Online (Sandbox Code Playgroud)

而且,如果我明确提供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]
Run Code Online (Sandbox Code Playgroud)

编译器最终显示出某种意义.

是否有任何类型的系统巫术我错过了导致b成为有效参数orElse?换句话说,怎么A1推断为String

如果编译器推断A1b那么它必须是Any,这样还有什么地方不来,导致推理链String开始?


更新

在使用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>
Run Code Online (Sandbox Code Playgroud)

由于(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
Run Code Online (Sandbox Code Playgroud)

这就是混合StringAny结果的原因String.

话虽如此,引擎盖下发生了什么?统一不匹配类型的逻辑是什么?

更新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]
Run Code Online (Sandbox Code Playgroud)

Gab*_*lla 4

你把这个搞颠倒了。

您始终可以向需要类型参数的方法传递A任何类型的参数B <: A,即任何子类型A。也就是说,如果你有

def foo(a: Animal)
Run Code Online (Sandbox Code Playgroud)

你可以传递一个Dogto foo,因为Dog <: Animal.

同样的,如果你有

def foo(l: List[Animal])
Run Code Online (Sandbox Code Playgroud)

你可以将 a 传递List[Dog]给它,因为List是 与其类型参数协变的,并且既然Dog <: Animal,那么List[Dog] <: List[Animal]

现在如果你有

def foo(pf: PartialFunction[String, String])
Run Code Online (Sandbox Code Playgroud)

您可以传递 a PartialFunction[Any, String],因为PartialFunction与第一个类型参数是逆变的,与第二个类型参数是协变的。自Any >: String那以后PartialFuncion[Any, String] <: PartialFunction[String, String]

现在,对于类型界限,编译器将尝试推断A1B1,这样

  • A1是子类型A
  • B2是子类型B

为此,它将寻找:

  • Any和 的最常见子类型String,因为AA1处于逆变位置
  • String和 的最不常见的超类型String,因为BB1是协变位置

结果

  • A1String
  • B1String

PartialFunction[String, String将] 与 a组合的情况PartialFunction[Int, Int]是上一个示例中看起来很奇怪的情况,其中:

  • String和 的最常见子类型IntString with Int,即两种类型的交集,它是两者的子类型(在这种情况下几乎等于说Nothing同时是aString和 anInt似乎不太可能)
  • String和的最不常见的超类型IntAny

所以

val a: PartialFunction[String, String] = ...
val b: PartialFunction[Int, Int] = ...
a orElse b // PartialFunction[String with Int, Any] // as expected, although not very useful...
Run Code Online (Sandbox Code Playgroud)

  • “你把事情搞颠倒了。” 你是用第二人称回答自己吗?:D。不管怎样,这似乎很好地概述了正在发生的事情。 (2认同)