> I tried by defining an implicit conversion from Bar to Foo in Bar�s
> companion object but that doesn�t work either. Even if we explicitly import
> the conversion.
The problem is the implicit *conversion*. Try this (note that the
parameter of the implicit is itself implicit):
trait Foo[A]
object Foo {
implicit def fromBar[A](implicit bar: Bar[A]) = bar.foo
}
trait Bar[A] {
def foo: Foo[A]
}
// usage
def f[A : Bar](a: A) = g(a)
def g[A : Foo](a: A) = a
Putting `fromBar` into `Bar`s companion object won't work, because when
calling `g`, the compiler doesn't look inside that object (by default).
Importing would work, though.
> We could use inheritance (as it is done in scalaz, IIRC):
>
> trait Foo[A]
> trait Bar[A] extends Foo[A]
>
> But to implement Bar you also would have to implement Foo�s members�
Do you control `Foo` and `Bar` in your own code? If so, I strongly
suggest going with the inheritance approach. It makes your life much easier.
If you really have the situation that you can't modify a `Foo` instance,
you can still have `Bar` extending `Foo`, and do something like this:
object Bar {
trait FromFoo[A] {
def foo: Foo[A]
// implement proxy methods to `foo`
def frobnicate = foo.frobnicate
// ...
}
}
and then you can write:
implicit object IntIsBar extends FromFoo[Int] {
// only implement `Bar` methods
}