Generically transforming a type in Scala -
is there way of transforming type in generic manner? have this:
class super { type messagestype = product type aggregatortype = product } class sub1 { override type messagestype = (class1, class2) override type aggregatortype = (option[class1], option[class2]) } class sub2 { override type messagestype = (class1, class2, class3) override type aggregatortype = (option[class1], option[class2], option[class3]) }
the aggregatortype
contain option
s of each of types in messagestype
. i've been looking @ ways of declaring messagestype
in sub
, having corresponding aggregatortype
generated automatically. while i'm using tuple
s, change hlist
s, case classes, or whatever. however, have limitation of using shapeless 1.2.4 (due transitive dependencies).
any ideas?
you use mapped
type class shapeless this.
it little bit difficult compiler infer of types. used nested class, maybe else can come prettier solution.
import shapeless._ import shapeless.ops.hlist.{mapped, tupler} class withmessage[message <: product] { class super[ g <: hlist, m <: hlist, t <: product ](implicit gen: generic.aux[message, g], // tuple generic (hlist) representation map: mapped.aux[g, option, m], // elements of g hlist in option tup: tupler.aux[m, t] // hlist m tuple ) { type aggregator = t // ... } }
which can used :
val stringintmsg = new withmessage[(string, int)] object si extends stringintmsg.super val si: si.aggregator = (some("foo"), some(1))
Comments
Post a Comment