在另一种方法中使用 Shapeless Poly

Using Shapeless Poly in another method

我正尝试在另一种方法中使用 Shapeless Poly

object poly extends Poly1 {
  implicit val caseInt = at[Int](_.toString)
  implicit val caseString = at[String](_.toString)
}

def f[A, P <: Poly](a: A, p: P) = println(p(a))

这给出了

could not find implicit value for parameter cse: shapeless.poly.Case[p.type,shapeless.::[A,shapeless.HNil]]

有什么关于如何让它工作的建议吗?

Poly.apply 需要 Case 隐式的隐式证据,这是您通过 at[A] 辅助方法提供的。

我们需要将相同的隐式要求添加到 f:

import shapeless._
import shapeless.PolyDefns.Case

def f[A, P <: Poly](a: A, p: P)(implicit cs: Case.Aux[p.type, shapeless.::[A, HNil], String]) = 
  println(p(a))