In fact, you are really close. You need to remove the parentheses from seqBoxer[A] . Otherwise, the compiler sees this as an implicit conversion from () => Boxer[Seq[A]] , and not just an available implicit Boxer[Seq[A]] . For a good measure, it is also useful to make the return type of the implicit method explicit.
implicit def seqBoxer[A]: Boxer[Seq[A]] = new Boxer[Seq[A]] { def box(instance: Seq[A]) = Box(instance) def unbox(box: Box[Seq[A]]) = box.value } scala> box(Seq(1, 2, 3)) res16: Box[Seq[Int]] = Box(List(1, 2, 3))
In fact, you can use the same approach to create a generic Boxer[A] for any A that should behave the same.
implicit def boxer[A]: Boxer[A] = new Boxer[A] { def box(instance: A): Box[A] = Box(instance) def unbox(box: Box[A]): A = box.value } scala> box("abc") res19: Box[String] = Box(abc) scala> box(List(1, 2, 3)) res20: Box[List[Int]] = Box(List(1, 2, 3)) scala> unbox(res20) res22: List[Int] = List(1, 2, 3) scala> box(false) res23: Box[Boolean] = Box(false)
source share