mardi 29 septembre 2015

Scala: What pattern should I use to create a common abstraction for Int, Double and String?

I need to refactor my whole system and I would like suggestions on how to deal with a problem with types.

I have this situation: I'm using PMML (basically an XML with a data schema specification) and HBase (a columnar storage that keeps no data type schema). I need to match the two while manipulating the values in my system. So I need to have an abstraction to handle all the possible data types that could be part of my system. Right now I have Int, Double and String.

For every type I need to be able to define a parser (for HBase), a writer and a set of operations defined as high order functions ((T,T)=>T).

Right now I tried to do that with a trait MyValue[T] that was extended by MyValueInt extends MyValue[Int] or MyValueDouble extends MyValue[Double]. I could define a list of operations like List[(MyValue,MyValue)=>MyValue] but I had to match it everytime with every case and created a lot of other problems with the type system.

What I need is a good abstraction to model the fact that these values can be only Int, Double and Strings, to create lists of these values and to be able to treat these values with a generic logic until I need to extract the real value and apply it to an operation.

Aucun commentaire:

Enregistrer un commentaire