How to extract an element from an HList with a specific (parameterized) type

白昼怎懂夜的黑 提交于 2019-12-06 02:56:09

You're exactly right about needing evidence that at.Out is A, and you can provide that evidence by including the value of the type member in at's type:

def mapNth[L <: HList, A, B]
    (l: L, index: Nat, f: A => B)
    (implicit at: shapeless.ops.hlist.At[L, index.N] { type Out = A }):
    B :: L =
  f(l(index)) :: l

The companion objects for type classes like At in Shapeless also define an Aux type that includes the output type as a final type parameter.

def mapNth[L <: HList, A, B]
    (l: L, index: Nat, f: A => B)
    (implicit at: shapeless.ops.hlist.At.Aux[L, index.N, A]):
    B :: L =
  f(l(index)) :: l

This is pretty much equivalent but more idiomatic (and it looks a little nicer).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!