scala - Reverse inheritance -
for custom dsl, have set of data producers , consumers. (it's little more complicated that, suffice set problem.) users set consumer, may require type of data. make sure type system enforces correct type of data sent correct consumer.
the possible elaborations of data bounded inasmuch possible write single data producer can @ least fill in sensible default values. thus, there should lower bound data types consists of supplier of default data. also, data shares characteristics, there should upper bound. , data can manipulated in ways preserve suitability given consumer, or widen suitability more consumers. thus, types should like
alldata <: d <: anydata
what (or most) compact , elegant way encode constraint in scala, assuming not want require alldata
extend every other data type d
?
right have superclass looks like
class anydata { def foo: int => string = defaultinttostringimpl def mapfoo(oo: string => string) = alldata((foo _) andthen oo) }
and subclasses can override it
class literalnumberdata extends anydata { override val foo = (i: int) => i.tostring }
and there case class allows every method pluggable
case class alldata(override val foo: int => string) {}
but consumers choose between anydata
, alldata
if they're going allow mappings, because mappings end alldata
. doesn't provide safety consumers i'd like, , it's me make sure remember not in e.g. literalnumberdata
cannot packed alldata
.
i make constraint on types follows:
trait prodconsconstraint[producer,consumer]{ type output <: anydata }
if i'm following correctly. use dependent types enforce suitability of types you're working with.
def foo(prod: producer, con: consumer)(implicit ev:prodconsconstraint[producer,consuer]): ev.output = ???
this removes need lower bound while allowing leeway "open" things via scoping of implicit parameters.
hmm... sounds overly complex. maybe i'm not understanding question enough.
Comments
Post a Comment