1

我有一个依赖于的类HList,有一个多态函数和一个将这个函数映射到列表上的方法。奇怪的是,这个方法只能在 a 上调​​用val,否则编译失败:

import shapeless._
import shapeless.ops.hlist.Mapper

class C[L <: HList](l: L) {
    object f extends Poly1 {
        implicit def whatever[T] = at[T]{_ => ()}
    }
    def g(implicit m: Mapper[f.type,L]) = ()
}
val c = new C(1 :: "s" :: HNil)

// this line compiles:
val v1 = c.g

// compilation fails if c is inlined:
val v2 = (new C(1 :: "s" :: HNil)).g
// error: could not find implicit value for parameter m:
//   Mapper[_1.f.type, Int :: String :: HNil]]]

有没有办法在保持f内部的同时克服这种行为C?我试图将该方法提取g到另一个类或对象,但它没有帮助。

4

0 回答 0