Heterogeneous arguments in a Scala function

2019-02-09 17:03发布

问题:

How can I pass some HList as an argument? So I can make in a such way:

def HFunc[F, S, T](hlist: F :: S :: T :: HNil) {
    // here is some code
}

HFunc(HList(1, true, "String")) // it works perfect

But if I have a long list, and I dunno nothing about it, how can I make some operations on it? How can I pass argument and not to loose its type?

回答1:

It depends on your use-case.

HList is useful for type-level code, so you should pass to your method not only HList, but also all necessary information like this:

def hFunc[L <: HList](hlist: L)(implicit h1: Helper1[L], h2: Helper2[L]) {
    // here is some code
}

For instance if you want to reverse your Hlist and map over result you should use Mapper and Reverse like this:

import shapeless._, shapeless.ops.hlist.{Reverse, Mapper}

object negate extends Poly1 {
  implicit def caseInt = at[Int]{i => -i}
  implicit def caseBool = at[Boolean]{b => !b}
  implicit def caseString = at[String]{s => "not " + s}
}

def hFunc[L <: HList, Rev <: HList](hlist: L)(
                              implicit rev: Reverse[L]{ type Out = Rev },
                                       map: Mapper[negate.type, Rev]): map.Out =
  map(rev(hlist)) // or hlist.reverse.map(negate)

Usage:

hFunc(HList(1, true, "String"))
//String :: Boolean :: Int :: HNil = not String :: false :: -1 :: HNil