具有泛型的 Scala 类型类
Scala Typeclasses with generics
我一直在研究 Scala 中的类型类模式,但我一直无法弄清楚当我使用的类型是泛型时如何实现隐式伴随对象。
例如,假设我已经为类型类定义了一个特征,它提供了将事物放入 Box
es 的函数。
case class Box[A](value: A)
trait Boxer[A] {
def box(instance: A): Box[A]
def unbox(box: Box[A]): A
}
implicit object IntBoxer extends Boxer[Int] {
def box(instance: Int) = Box(instance)
def unbox(box: Box[Int]) = box.value
}
def box[A : Boxer](value: A) = implicitly[Boxer[A]].box(value)
def unbox[A : Boxer](box: Box[A]) = implicitly[Boxer[A]].unbox(box)
这按预期工作,允许我为各种类型提供 Boxer
的实现。但是,我没有 知道当我希望操作的类型本身是泛型时我将如何执行此操作。假设我希望能够在任何 Seq[A]
上使用我的 Boxer
。 object
Scala 中的 s 不能包含类型参数,所以我不知道该去哪里:
// Will not compile - object cannot have type arguments
implicit object SeqBoxer[A] extends Boxer[Seq[A]] { ... }
// Will not compile - 'A' is unrecognized
implicit object SeqBoxer extends Boxer[Seq[A]] { ... }
// Compiles but fails on execution, as this doesn't implement an implicit
// conversion for _specific_ instances of Seq
implicit object SeqBoxer extends Boxer[Seq[_]] {
def box(instance: Seq[_]) = Box(instance)
def unbox(box: Box[Seq[_]]) = box.value
}
// Will not compile - doesn't technically implement Boxer[Seq[_]]
implicit object SeqBoxer extends Boxer[Seq[_]] {
def box[A](instance: Seq[A]) = Box(instance)
def unbox[A](box: Box[Seq[A]]) = box.value
}
// Compiles, but won't resolve with 'implicitly[Boxer[Seq[Foo]]]'
// I had high hopes for this one, too :(
implicit def seqBoxer[A]() = new Boxer[Seq[A]] {
def box(instance: Seq[A]) = Box(instance)
def unbox(box: Box[Seq[A]]) = box.value
}
有没有什么方法可以支持泛型类型的隐式转换,而不必为 each 内部类型隐式单独的对象?
实际上,你真的很接近。您需要从 seqBoxer[A]
中删除括号。否则,编译器会将其视为从 () => Boxer[Seq[A]]
的隐式转换,而不是简单的可用隐式 Boxer[Seq[A]]
。为了更好地衡量,将隐式方法的 return 类型显式化也是一个好主意。
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))
您实际上可以使用相同的方法为任何 A
创建通用的 Boxer[A]
,应该要求以相同的方式运行。
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)
我一直在研究 Scala 中的类型类模式,但我一直无法弄清楚当我使用的类型是泛型时如何实现隐式伴随对象。
例如,假设我已经为类型类定义了一个特征,它提供了将事物放入 Box
es 的函数。
case class Box[A](value: A)
trait Boxer[A] {
def box(instance: A): Box[A]
def unbox(box: Box[A]): A
}
implicit object IntBoxer extends Boxer[Int] {
def box(instance: Int) = Box(instance)
def unbox(box: Box[Int]) = box.value
}
def box[A : Boxer](value: A) = implicitly[Boxer[A]].box(value)
def unbox[A : Boxer](box: Box[A]) = implicitly[Boxer[A]].unbox(box)
这按预期工作,允许我为各种类型提供 Boxer
的实现。但是,我没有 知道当我希望操作的类型本身是泛型时我将如何执行此操作。假设我希望能够在任何 Seq[A]
上使用我的 Boxer
。 object
Scala 中的 s 不能包含类型参数,所以我不知道该去哪里:
// Will not compile - object cannot have type arguments
implicit object SeqBoxer[A] extends Boxer[Seq[A]] { ... }
// Will not compile - 'A' is unrecognized
implicit object SeqBoxer extends Boxer[Seq[A]] { ... }
// Compiles but fails on execution, as this doesn't implement an implicit
// conversion for _specific_ instances of Seq
implicit object SeqBoxer extends Boxer[Seq[_]] {
def box(instance: Seq[_]) = Box(instance)
def unbox(box: Box[Seq[_]]) = box.value
}
// Will not compile - doesn't technically implement Boxer[Seq[_]]
implicit object SeqBoxer extends Boxer[Seq[_]] {
def box[A](instance: Seq[A]) = Box(instance)
def unbox[A](box: Box[Seq[A]]) = box.value
}
// Compiles, but won't resolve with 'implicitly[Boxer[Seq[Foo]]]'
// I had high hopes for this one, too :(
implicit def seqBoxer[A]() = new Boxer[Seq[A]] {
def box(instance: Seq[A]) = Box(instance)
def unbox(box: Box[Seq[A]]) = box.value
}
有没有什么方法可以支持泛型类型的隐式转换,而不必为 each 内部类型隐式单独的对象?
实际上,你真的很接近。您需要从 seqBoxer[A]
中删除括号。否则,编译器会将其视为从 () => Boxer[Seq[A]]
的隐式转换,而不是简单的可用隐式 Boxer[Seq[A]]
。为了更好地衡量,将隐式方法的 return 类型显式化也是一个好主意。
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))
您实际上可以使用相同的方法为任何 A
创建通用的 Boxer[A]
,应该要求以相同的方式运行。
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)