我正在尝试实现简单的类型类模式。它应该类似于scalaz的类型类。不幸的是,我无法让它工作。我有一个特质Str
trait Str[T] {
def str(t: T): String
}
object Str {
def apply[T](implicit instance: Str[T]) : Str[T] = instance
}
在我的和隐式实例中。
object Temp extends App {
implicit val intStr = new Str[Int] {
def str(i: Int) = i.toString
}
1.str //error: value str is not a member of Int
}
我很感激任何关于IT技术的见解。
Str(1).str
吗?你没有定义一个implicit class
,只是一个带有apply
的object
。 - Gábor Bakos