如何映射扩展共同特征的对象列表?

How to map over Hlist of objects that extend common trait?

我有一个特征和扩展它的对象。

trait Common[K] {
  def name: String
  def encode(k: K): String = name + k.toString
}

object A extends Common[Int] {
  override def name: String = "a"
}

object B extends Common[Int] {
  override def name: String = "b"
}

object C extends Common[Int] {
  override def name: String = "c"
}

我想创建这个对象的 hlist 并在其上映射:

val hl = A :: B :: C :: HNil
val result: List[Int => String] = hl.map(EncodePoly).toList

以及实现 Poly 函数的不同尝试:

object EncodePoly extends Poly1 {
  implicit def indCase[K]: Case.Aux[Common[K], K => String] = at[Common[K]] {
    common => k =>
      common.encode(k)
  }
}

object EncodePoly extends Poly1 {
  implicit def indCase[K, C <: Common[K]]: Case.Aux[C, K => String] = at[C] {
    common => k =>
      common.encode(k)
  }
}

编译器告诉我:

Error:(45, 43) could not find implicit value for parameter mapper: shapeless.ops.hlist.Mapper[com.test.EncodePoly.type,com.test.A.type :: com.test.B.type :: com.test.C.type :: shapeless.HNil] val result: List[Int => String] = hl.map(EncodePoly).toList

我也尝试过使用依赖类型作为共同特征而不是类型参数。似乎没有任何效果。我应该如何使用 hlist of objects?

你的第二个 EncodePoly 很接近,但是编译器不够聪明,无法推断 K 应该是 Int 然后 C 应该是单例类型.您可以通过使用 <:< 而不是 <::

对子类型关系进行编码来帮助进行类型推断
trait Common[K] {
  def name: String
  def encode(k: K): String = name + k.toString
}

object A extends Common[Int] {
  override def name: String = "a"
}

object B extends Common[Int] {
  override def name: String = "b"
}

object C extends Common[Int] {
  override def name: String = "c"
}

import shapeless.{ ::, HNil, Poly1 }

object EncodePoly extends Poly1 {
  implicit def indCase[K, C](implicit ev: C <:< Common[K]): Case.Aux[C, K => String] = at[C] {
    common => k => common.encode(k)
  }
}

然后:

scala> val hl = A :: B :: C :: HNil
hl: A.type :: B.type :: C.type :: shapeless.HNil = A$@3f044518 :: B$@282b7aad :: C$@7c130749 :: HNil

scala> val result: List[Int => String] = hl.map(EncodePoly).toList
result: List[Int => String] = List(EncodePoly$$$Lambda55/1493211716@7c987ea3, EncodePoly$$$Lambda55/1493211716@10be689, EncodePoly$$$Lambda55/1493211716@5dd3c2f2)

如果您可以在 EncodePoly 定义中将 K 修改为 Int,那也行。