Heterogeneous arguments in a Scala function

孤街浪徒 提交于 2019-11-29 19:22:35

问题


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


来源:https://stackoverflow.com/questions/19634394/heterogeneous-arguments-in-a-scala-function

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