如何使类型类与Scala中的异构列表一起使用 [英] How to make a typeclass works with an heterogenous List in scala
问题描述
给出以下类型类和一些常见类型的实例
Given the following typeclass and some instances for common types
trait Encoder[A] {
def encode(a: A): String
}
object Encoder {
implicit val stringEncoder = new Encoder[String] {
override def encode(a: String): String = a
}
implicit val intEncoder = new Encoder[Int] {
override def encode(a: Int): String = String.valueOf(a)
}
implicit def listEncoder[A: Encoder] =
new Encoder[List[A]] {
override def encode(a: List[A]): String = {
val encoder = implicitly[Encoder[A]]
a.map(encoder.encode).mkString(",")
}
}
}
有没有办法使它起作用
Encoder.listEncoder.encode(List("a", 1))
推荐答案
如果为 Any
object Encoder {
implicit val anyEncoder = new Encoder[Any] {
override def encode(a: Any): String = a.toString
}
//instances for String, Int, List[A]
}
然后
Encoder.listEncoder[Any].encode(List("a", 1))
将起作用.
您必须在此处明确指定类型参数( listEncoder [Any]
),因为这就是在scala中类型推断的工作方式.确实,在推断出 encode
的参数类型
You have to specify type parameter here explicitly (listEncoder[Any]
) because that's how type inference work in scala. Indeed, after the type of argument of encode
is inferred
Encoder.listEncoder[???].encode(List("a", 1))
// ^^^^^^^^^^^^
// List[Any]
现在回来推断 listEncoder
的类型参数为时已晚.
it's too late to come back to infer the type parameter of listEncoder
.
如果定义了委托人功能
def encode[A](a: A)(implicit encoder: Encoder[A]): String = encoder.encode(a)
或语法
implicit class EncoderOps[A](a: A)(implicit encoder: Encoder[A]) {
def encode: String = encoder.encode(a)
}
// or
// implicit class EncoderOps[A](a: A) {
// def encode(implicit encoder: Encoder[A]): String = encoder.encode(a)
// }
那么您不必显式指定类型参数
then you don't have to specify type parameter explicitly
encode("a")
encode(1)
encode(List("a", 1))
"a".encode
1.encode
List("a", 1).encode
顺便说一句, List("a",1)
不是异类列表,它是元素类型为 Any
的普通同质列表.异构列表将是
By the way, List("a", 1)
is not a heterogenous list, it's an ordinary homogenous list with element type Any
. A heterogenous list would be
val l: String :: Int :: HNil = "a" :: 1 :: HNil
您可以尝试磁铁,而不是或 HList
而不是 List [A]
sealed trait HList
case class ::[+H, +T <: HList](head: H, tail: T) extends HList
case object HNil extends HList
type HNil = HNil.type
implicit class HListOps[L <: HList](l: L) {
def ::[A](a: A): A :: L = new ::(a, l)
}
trait Encoder[A] {
def encode(a: A): String
}
object Encoder {
implicit val stringEncoder: Encoder[String] = new Encoder[String] {
override def encode(a: String): String = a
}
implicit val intEncoder: Encoder[Int] = new Encoder[Int] {
override def encode(a: Int): String = String.valueOf(a)
}
implicit val hnilEncoder: Encoder[HNil] = new Encoder[HNil] {
override def encode(a: HNil): String = ""
}
implicit def hconsEncoder[H, T <: HList](implicit
hEncoder: Encoder[H],
tEncoder: Encoder[T]
): Encoder[H :: T] = new Encoder[H :: T] {
override def encode(a: H :: T): String =
s"${hEncoder.encode(a.head)},${tEncoder.encode(a.tail)}"
}
}
def encode[A](a: A)(implicit encoder: Encoder[A]): String = encoder.encode(a)
implicit class EncoderOps[A](a: A)(implicit encoder: Encoder[A]) {
def encode: String = encoder.encode(a)
}
val l: String :: Int :: HNil = "a" :: 1 :: HNil
encode(l)
l.encode
这篇关于如何使类型类与Scala中的异构列表一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!