You can, in a fairly round about way. Foo
is a type class, and the compiler implcitly passes an instance of the type class, compatible with the (inferred) type parameter A
.
trait Foo[X] {
def apply(xs: Seq[X]): Unit
}
object Foo {
implicit def FooAny[A]: Foo[A] = new Foo[A] {
def apply(xs: Seq[A]) = println("apply(xs: Seq[A])")
}
implicit def FooTuple2[A, B]: Foo[(A, B)] = new Foo[(A, B)] {
def apply(xs: Seq[(A, B)]) = println("apply(xs: Seq[(A, B)])")
}
def apply[A](xs: A*)(implicit f: Foo[A]) = f(xs)
}
Foo(1, 2, 3) // apply(xs: Seq[A])
Foo(1 -> 2, 2 -> 3) // apply(xs: Seq[(A, B)])
In the second call, both FooAny
and FooTuple2
could be passed, but the compiler picks FooTuple2
, based on the rules of static method overloading. FooTuple2
is considered more specific that FooAny
. If two candidates are considered to be as specific as each other, an ambiguity error is raised. You can also prefer one over the other by placing one in a superclass, as is done in scala.LowPriorityImplicits
.
UPDATE
Riffing off the DummyImplicit idea, and the followup thread on scala-user:
trait __[+_]
object __ {
implicit object __ extends __[Any]
}
object overload {
def foo(a: Seq[Boolean]) = 0
def foo[_: __](a: Seq[Int]) = 1
def foo[_: __ : __](a: Seq[String]) = 2
}
import overload._
foo(Seq(true))
foo(Seq(1))
foo(Seq("s"))
This declares a type-parameterized trait __
, covariant in its unnamed type parameter _
. Its companion object __
contains an implicit instance of __[Any]
, which we'll need later on. The second and third overloads of foo
include a dummy type parameters, again unnamed. This will be inferred as Any
. This type parameter has one or more context bounds, which are desugared into additional implicit parameters, for example:
def foo[A](a: Seq[Int])(implicit ev$1: __[A]) = 1
The multiple parameter lists are concatenated into a single parameter list in the bytecode, so the double definition problem is circumvented.
Please consider this as a opportunity to learn about erasure, context bounds and implicit search, rather than as a pattern to be applied in real code!