scala - Can I use shapeless to return the same arity of HList as passed HList? -


here example. i'm trying wrap external api accepts , returns same arity of list:

def externapi(args: list[int]): list[string] = args.map(_.tostring) 

i thought excuse learn shapeless seems hlist able do.

def foo(args: hlist): hlist = ??? 

how can encode in type passed hlist , returned hlist of same arity?

to expand on @stew's comment, can use sized enforce equal arity between lists.

import shapeless._ import syntax.sized._  def externapi[n <: nat](args: sized[list[int], n]): sized[list[string], n] =     args.map(_.tostring) 

usage:

scala> externapi(sized[list](1, 2, 3, 4)) res0: shapeless.sized[list[string],shapeless.nat._4] = list(1, 2, 3, 4)  scala> res0 foreach println 1 2 3 4 

i'm far shapeless expert, don't know if there way hlist, seems collections homogeneous anyway.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -