I avoid overloading, except maybe in the outermost layer of an API where it might benefit end users. But having said that, there’s a trick you can use. There is a thing called DummyImplicit
which has a value that’s always available implicitly, and you can add it to change a method’s arity so you can distinguish methods that would otherwise be identical due to erasure.
For example, this compiles and you can call all three methods normally, but only because the methods actually differ in arity. If you remove the implicit
arg lists it won’t compile anymore.
def foo(as: List[Int]) = as.length
def foo(as: List[String])(implicit ev: DummyImplicit) = 42
def foo(as: List[Boolean])(implicit ev1: DummyImplicit, ev2: DummyImplicit) = -1
foo(List(1, 2))
foo(List("foo", "bar"))
foo(List(true))
I can’t really recommend using this as a matter of practice but it can get you out of a pinch.