Friday, 9 August 2013

Implicit conversion to supertype using type classes

Implicit conversion to supertype using type classes

Why does foo1 fail and foo2 succeeds? Shouldn't the compiler automatically
check all the supertypes of Blah?
trait Foo[A] {
def bar: A
}
trait Bleh;
case class Blah extends Bleh;
implicit object BlehFoo extends Foo[Bleh]
def foo1[A:Foo](a:A) = a
def foo2[A,B:Foo](a:A)(implicit aToB: A => B) = aToB(a)
// Shouldn't it automatically use Bleh?
foo1(Blah())
// Failure: could not find implicit value for evidence parameter of type
Foo[Blah]
foo2(Blah())
// Success: Bleh = Blah()

No comments:

Post a Comment