scalaimplicitshapelesshlist

HList Ops - how are type classes constracted?


Can someone explain why this code compiles and works like a charm:

val a = true :: Some(5) :: true :: HNil
a.select[Some[Int]]                                // Some(5)

but this one fails:

def foo[HL <: HList](a: HL): Some[Int] = {
  a.select[Some[Int]]                              // fails
}
Implicit not found: shapeless.Ops.Selector[tp, Some[Int]]. You requested an element of type Some[Int], but there is none in the HList tp.
a.select[Some[Int]]

I don't want to pass a instance of Selector.

I also tried:


type tp = Boolean :: Option[Int] :: Boolean :: HNil
def foo(a: tp): Some[Int] = a.select[Some[Int]]       // fails
foo(a)

but it failed with a message:

Implicit not found: shapeless.Ops.Selector[tp, Some[Int]]. You requested an element of type Some[Int], but there is none in the HList tp.
a.select[Some[Int]]

Solution

  • How about this:

    def foo[HL <: HList](a: HL)
                        (implicit sel: ops.hlist.Selector[HL, Some[Int]]): Some[Int] = {
      a.select[Some[Int]]                       
    }
    

    The full signature of select is

    def select[U](implicit selector: hlist.Selector[HL,U]): U
    

    There is an implicit parameter selector, so you must provide one.