Scala: implicitly convert Option to Option of implicit class

Viewed 130

How to implicitly convert Option[A] to Option[B], where B is an implicit class of A?

In the following code only converter1 works, but the return type is not generic:

implicit class Test(val i: Int)

implicit def converter0[A, B](opt: Option[A])(implicit f: A => B): Option[B] = {
  opt.map(f)
}

implicit def converter1[A](opt: Option[A])(implicit f: A => Test): Option[Test] = {
  opt.map(f)
}

implicit def converter2[B](opt: Option[Int])(implicit f: Int => B): Option[B] = {
  opt.map(f)
}

val b: Option[Int] = Option(1)
val a: Option[Test] = b
0 Answers
Related