您使用Aux 模式的原因在您的示例中没有明确定义。当Aux 提供的类型的值尚不知道时,问题就出现了,这在大多数情况下意味着派生,更具体地说是宏/编译时类型派生。
这在Shapeless中被广泛使用。
def test[V1, HL](input: V1)(implicit ev: Generic.Aux[V1, HL])`
当你写这篇文章时,你可能会认为HL 是Aux 的输入类型,但它实际上是Generic 宏内的输出,作为Aux 的消耗品提供,例如输出是神奇的“注入”到 HL 中,因此由 Generic.Aux 计算的输出类型可通过该调用站点之外的 HL 访问。
如果您在同一个参数组中有多个隐式,则存在绕过编译器限制,其中一个隐式取决于前一个的 Aux 宏输出。
从The Scala Bible, New Testament, Gospel of Miles粘贴:
* More importantly, Aux allows us to write code like this:
*
* {{{
* def myMethod[T, R]()(implicit eqGen: Generic.Aux[T,R], repEq: Eq[R]) = ???
* }}}
*
* Here, we specify T, and we find a Generic.Aux[T,R] by implicit search. We then use R in the second argument.
* Generic.Aux[T, R] is exactly equivalent to Generic[T] { type Repr = R }, but Scala doesn't allow us to write
* it this way:
*
* {{{
* def myMethod[T, R]()(eqGen: Generic[T] { Repr = R }, reqEq: Eq[egGen.Repr]) = ???
* }}}