我想使用Shapeless 库中的SYB实现来编写以下通用遍历函数:
class Data
// Perform the desired manipulation on the given data
object manipulate extends ->((data: Data) => data)
def traverseAndManipulate[B](expr: B): B = {
everywhere(manipulate)(expr)
}
不幸的是,此代码产生以下类型错误(使用 Shapeless 2.0.0-M1 和 Scala 2.10.2):
type mismatch;
[error] found : shapeless.EverywhereAux[SYB.manipulate.type]
[error] required: ?{def apply(x$1: ? >: B): ?}
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error] both method inst1 in trait PolyInst of type [A](fn: shapeless.Poly)(implicit cse: fn.ProductCase[shapeless.::[A,shapeless.HNil]])A => cse.Result
[error] and macro method apply in object Poly of type (f: Any)shapeless.Poly
[error] are possible conversion functions from shapeless.EverywhereAux[SYB.manipulate.type] to ?{def apply(x$1: ? >: B): ?}
[error] everywhere(manipulate)(expr)
我假设B
需要以某种方式限制类型参数,以使 Shapeless 库的隐式宏适用,但我不知道如何。
这样的遍历函数能用Shapeless写吗?