Let's define a PartialFunction[String, String]
and a PartialFunction[Any, String]
Now, given the definition of orElse
def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]): PartialFunction[A1, B1]
I would expect not to be able to compose the the two, since
A
→ String
A1
→ Any
and therefore the bound A1 <: A
(i.e. Any <: String
) doesn't hold.
Unexpectedly, I can compose them and obtain a PartialFunction[String, String]
defined on the whole String
domain. Here's an example:
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
Moreover, if I explicitly provide the orElse
type parameters
a orElse[Any, String] b
// error: type arguments [Any,String] do not conform to method orElse's type parameter bounds [A1 <: String,B1 >: String]
the compiler finally shows some sense.
Is there any type system sorcery I'm missing that causes b
to be a valid argument for orElse
? In other words, how come that A1
is inferred as String
?
If the compiler infers A1
from b
then it must be Any
, so where else does the inference chain that leads to String
start?
Update
After playing with the REPL I noticed that orElse
returns an intersection type A with A1
when the types don't match. Example:
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
this works, even though the resulting function is practically unusable. I also suspect that String with Any
is unified into Any
, given that
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
So that's why mixing String
and Any
results into String
.
That being said, what is going on under the hood? Under which logic are the mismatching types unified?
Update 2
I've reduced the issue to a more general form:
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]