我正在使用 Cats 使用 Scala,并且我正在尝试创建Show 一个Tree[A]类型(不是书本练习)。基本上,我正在尝试创建一个实例,这样如果我有任何树,其基础值有一个Show我可以调用的实例tree.show。我正在为如何表示这一点而苦苦挣扎。
到目前为止,我有
sealed trait Tree[+A]
final case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]
final case class Leaf[A](value: A) extends Tree[A]
object Tree {
implicit val show: Show[Tree[Show[_]]] = new Show[Tree[Show[_]]] {
def show(t: Tree[Show[_]]): String = t match {
case Branch(left, right) => s"*\n/\n${show(left)} ${show(right)}\n"
case Leaf(value) => value.show
}
}
}
我得到missing argument list for method show in trait ContravariantShow
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing show _ or show(_) instead of展示.
当我按原样重写value.show(_)时type mismatch; found : _$3 => String required: String
当我只写一个字符串"value"来测试时,我的Tree[Int]实例无法show在 type 上找到方法Tree[Int]。
关于如何完成这项工作的任何想法?