One way that has been suggested to deal with double definitions of overloaded methods is to replace overloading with pattern matching:
object Bar {
def fo
We’d like a type operator Or[U,V]
that can be used to constrain a type parameters X
in such a way that either X <: U
or X <: V
. Here's a definition that comes about as close as we can get:
trait Inv[-X]
type Or[U,T] = {
type pf[X] = (Inv[U] with Inv[T]) <:< Inv[X]
}
Here is how it's used:
// use
class A; class B extends A; class C extends B
def foo[X : (B Or String)#pf] = {}
foo[B] // OK
foo[C] // OK
foo[String] // OK
foo[A] // ERROR!
foo[Number] // ERROR!
This uses a few Scala type tricks. The main one is the use of generalized type constraints. Given types U
and V
, the Scala compiler provides a class called U <:< V
(and an implicit object of that class) if and only if the Scala compiler can prove that U
is a subtype of V
. Here’s a simpler example using generalized type constraints that works for some cases:
def foo[X](implicit ev : (B with String) <:< X) = {}
This example works when X
an instance of class B
, a String
, or has a type that is neither a supertype nor a subtype of B
or String
. In the first two cases, it’s true by the definition of the with
keyword that (B with String) <: B
and (B with String) <: String
, so Scala will provide an implicit object that will be passed in as ev
: the Scala compiler will correctly accept foo[B]
and foo[String]
.
In the last case, I’m relying on the fact that if U with V <: X
, then U <: X
or V <: X
. It seems intuitively true, and I’m simply assuming it. It’s clear from this assumption why this simple example fails when X
is a supertype or subtype of either B
or String
: for example, in the example above, foo[A]
is incorrectly accepted and foo[C]
is incorrectly rejected. Again, what we want is some kind of type expression on the variables U
, V
, and X
that is true exactly when X <: U
or X <: V
.
Scala’s notion of contravariance can help here. Remember the trait trait Inv[-X]
? Because it is contravariant in its type parameter X
, Inv[X] <: Inv[Y]
if and only if Y <: X
. That means that we can replace the example above with one that actually will work:
trait Inv[-X]
def foo[X](implicit ev : (Inv[B] with Inv[String]) <:< Inv[X]) = {}
That’s because the expression (Inv[U] with Inv[V]) <: Inv[X]
is true, by the same assumption above, exactly when Inv[U] <: Inv[X]
or Inv[V] <: Inv[X]
, and by the definition of contravariance, this is true exactly when X <: U
or X <: V
.
It’s possible to make things a little more reusable by declaring a parametrizable type BOrString[X]
and using it as follows:
trait Inv[-X]
type BOrString[X] = (Inv[B] with Inv[String]) <:< Inv[X]
def foo[X](implicit ev : BOrString[X]) = {}
Scala will now attempt to construct the type BOrString[X]
for every X
that foo
is called with, and the type will be constructed precisely when X
is a subtype of either B
or String
. That works, and there is a shorthand notation. The syntax below is equivalent (except that ev
must now be referenced in the method body as implicitly[BOrString[X]]
rather than simply ev
) and uses BOrString
as a type context bound:
def foo[X : BOrString] = {}
What we’d really like is a flexible way to create a type context bound. A type context must be a parametrizable type, and we want a parametrizable way to create one. That sounds like we’re trying to curry functions on types just like we curry functions on values. In other words, we’d like something like the following:
type Or[U,T][X] = (Inv[U] with Inv[T]) <:< Inv[X]
That’s not directly possible in Scala, but there is a trick we can use to get pretty close. That brings us to the definition of Or
above:
trait Inv[-X]
type Or[U,T] = {
type pf[X] = (Inv[U] with Inv[T]) <:< Inv[X]
}
Here we use structural typing and Scala’s pound operator to create a structural type Or[U,T]
that is guaranteed to have one internal type. This is a strange beast. To give some context, the function def bar[X <: { type Y = Int }](x : X) = {}
must be called with subclasses of AnyRef
that have a type Y
defined in them:
bar(new AnyRef{ type Y = Int }) // works!
Using the pound operator allows us to refer to the inner type Or[B, String]#pf
, and using infix notation for the type operator Or
, we arrive at our original definition of foo
:
def foo[X : (B Or String)#pf] = {}
We can use the fact that function types are contravariant in their first type parameter in order to avoid defining the trait Inv
:
type Or[U,T] = {
type pf[X] = ((U => _) with (T => _)) <:< (X => _)
}