Consider the following code:
object foo {
trait Bar[Q[_]]
implicit object OptionBar extends Bar[Option]
def test[T, C[_]](c: C[T])(implicit ba
That's because Some(42)
is a more specific type than Option[Int]
. It is a Some[Int]
. See alternative coding below:
object foo {
trait Bar[Q[_]]
implicit object OptionBar extends Bar[Option]
def test[T, C[_]](c: C[T])(implicit bar: Bar[C]) = ()
def main(args: Array[String]) {
test(Option(42))
}
}
It's not going to work in all cases, but as stated, you can try this:
object foo {
trait Bar[Q[_]]
implicit object OptionBar extends Bar[Option]
def test[T, C[_], D](c: D)(implicit bar: Bar[C], ev: D <:< C[T]) = ()
def main(args: Array[String]) {
test(Some(42)) //???
}
}
Interestingly, this doesn't infer, although it expresses the same thing:
def test[T, C[_], D <: C[T]](c: D)(implicit bar: Bar[C]) = ()
To learn more about <:<
, see: