I have the following Scala code snippet:
import java.io.PrintWriter trait Write[-A] { def apply(out: PrintWriter)(x: A): Unit } trait LowPriorityWrites { implicit object any extends Write[Any] { override def apply(out: PrintWriter)(x: Any) = out.print(x) } } object Write extends LowPriorityWrites { implicit def iterable[A](implicit write: Write[A]): Write[Traversable[A]] = new Write[Traversable[A]] { override def apply(out: PrintWriter)(xs: Traversable[A]) = { xs foreach write(out) out.println() } } }
Basically, I want to get to first look for an implicit Write
object. If not, revert to the default .toString
for Any
of LowPriorityWrites
.
But this approach does not work:
ambiguous implicit values: [error] both method iterable in object Write of type [A](implicit write: Write[A])Write[Traversable[A]] [error] and object any in trait LowPriorityWrites of type Write.any.type [error] match expected type Write[Seq[Long]]
source share