我有一段代码,概念上类似于以下代码:
//library code
trait Support[K,V]
def partialHandler[K,V](key: K,value: V)(implicit ev: Support[K,V]) = ???
//user code
implicit val intIntSupport = new Support[Int,Int] {}
implicit val intStringSupport = new Support[Int,String] {}
...
partialHandler(1,"foo)
partialHandler(1,1)
我想知道是否有一种方法可以让该库的用户更优雅地定义受支持的(K,V)
类型,例如:
val supportedTypes = new Support[Int,Int] {} :: new Support[Int,String] {} :: HNil
(本质上,我正在寻找从几乎未知的HList到Support[K,V]
的隐式转换。这看起来不可行,但也许我遗漏了某些东西。)