HList 的无形类型推断不起作用

Shapeless type inference for HList does not work

我正在尝试实现获取第一个元素的通用函数:

import shapeless.ops.hlist.IsHCons
import shapeless.{Generic, HList}

object App {

  def main(args : Array[String]) {
    val a: Int = getFirst((1, 2, 3))
    val b: String = getFirst(("a", "b", 10, 45, "aaa"))
  }

  def getFirst[A, Head, Repr <: HList, Tail <: HList](input: A)
                                                     (implicit hcons: IsHCons.Aux[Repr, Head, Tail],
                                                      gen: Generic.Aux[A, Repr]): Head = gen.to(input).head
}

问题是编译器无法正确推断 ReprTail 并将它们设置为 Nothing。在这里:

Error:(9, 26) could not find implicit value for parameter gen: shapeless.Generic.Aux[(Int, Int, Int),Repr]
    val a: Int = getFirst((1, 2, 3))
Error:(9, 26) not enough arguments for method getFirst: (implicit hcons: shapeless.ops.hlist.IsHCons.Aux[Nothing :: Nothing,Head,Tail], implicit gen: shapeless.Generic.Aux[(Int, Int, Int),Nothing :: Nothing])Head.
Unspecified value parameter gen.
    val a: Int = getFirst((1, 2, 3))
Error:(10, 29) could not find implicit value for parameter gen: shapeless.Generic.Aux[(String, String, Int),Repr]
    val b: String = getFirst(("a", "b", 10))
Error:(10, 29) not enough arguments for method getFirst: (implicit hcons: shapeless.ops.hlist.IsHCons.Aux[Nothing :: Nothing,Head,Tail], implicit gen: shapeless.Generic.Aux[(String, String, Int),Nothing :: Nothing])Head.
Unspecified value parameter gen.
    val b: String = getFirst(("a", "b", 10))

修复它的解决方案是什么?以下肯定可以正常工作:

val a: Int = getFirst[(Int, Int, Int), Int, Int :: Int :: Int :: HNil, Int :: Int :: HNil]((1, 2, 3))

但这非常麻烦和丑陋。

解决方案是交换隐式参数顺序 - Generic 第一,IsHCons 第二。

  def getFirst[A, Head, Repr <: HList, Tail <: HList](input: A)
                                                     (implicit gen: Generic.Aux[A, Repr], hcons: IsHCons.Aux[Repr, Head, Tail]
                                                      ): Head = gen.to(input).head

(scastie)

我不知道这究竟是如何工作的,但我已经看到通过解析隐式来推断类型参数是从左到右工作的。

因此,根据您的签名,Scala 首先查找 IsHCons.Aux[Repr, Head, Tail],但此时它对 Repr 了解不多,只知道它是一个 HList。它找到 一些 实例 - 可能是 Nothing :: HList 之类的东西,然后尝试寻找 Generic.Aux[A, Nothing :: HList]A 是从 input 推断出来的),这会失败,因为这不是 A.

的有效 Generic