scala - Polymorphic Deserialization function using shapeless -
i'm trying write polymorphic deserialization function hlists/csv's using shapeless
here definition value typeclass provides serialization/deserialization (i'm aware results should return , option/either/validation show failed parsing, come later)
class value[t](val read: string => t, val write: t => string) object value { implicit object intvalue extends value[int](_.toint, _.tostring) implicit object stringvalue extends value[string](s => s, s => s) def apply[v: value] = implicitly[value[v]] } import value._
the following serialization function works great
object serialize extends poly1 { implicit def casevalue[t: value] = at[t](value[t].write) }
and im able map on hlist of types have supporting value[_] typeclass:
def write[l <: hlist, la1 <: hlist](t: l)( implicit mapper: mapper.aux[writepoly.type, l, la1], lister: tolist[la1, string]): list[string] = t.map(writepoly).tolist
here 2 different versions of deserialization function ive tried:
object deserialize extends poly1 { implicit def casevalue[t: value] = at[string](value[t].read) } object deserialize extends poly1 { implicit def casestr = at[string](value[string].read) implicit def caseint = at[string](value[int].read) }
and i'd use in function this
def read[l <: hlist, li <: hlist](t: li)( implicit mapper: mapper.aux[readpoly.type, li, l]): l = t.map(readpoly)
however when try map on hlist deserialize function, or when try use on own so:
deserialize("1")
the compiler complains ambiguous implicit values:
ambiguous implicit values: both method casestring in object deserialize of type => csvformat.deserialize.case[string]{type result = string} , method caseint in object deserialize of type => csvformat.deserialize.case[string]{type result = int} match expected type shapeless.poly.case[csvformat.deserialize.type,shapeless.::[string,shapeless.hnil]] print(deserialize[string]("a")) ^
my goal use function map on list of strings hlist of value[_] types in order serialize them, since getting same error while trying apply "deserialize" function on single string, i'm hoping solve problem first.
i'm struggling provide type hints lead compiler along here, since scala doesnt support type level currying, should using type lambda?
is there other way can express type of function?
Comments
Post a Comment