I've created (I think) an enumeration which has an associated type, under the constraint that the associated type has to be an implementation of the Numeric trait:
sealed trait DataType[T <: Numeric[T]] { type NumT = T }
trait Positions extends DataType[Double]
trait Values extends DataType[Double]
trait Weights extends DataType[Double]
trait Prices extends DataType[Double]
This seems all well and maybe even good, but I'd then like to use this enumeration in a case class, without having to duplicate the type parameter - so code in the spirit of this non-working example directly below:
case class SingleValue[DataT <: DataType] (
datum: DataT#NumT,
) {
def addToDatum(plus: DataT#NumT): SingleValue[DataT] = (this.datum += plus)
}
This doesn't compile, and I can't make it work - the compiler asks for a type parameter on DataType, but even if given one - like
case class SingleValue[T <: Numeric[T], DataT <: DataType[T]]
I am still hitting problems, and in this case I am repeating myself and the advantage of having an enumeration is mostly lost anyway.
Could anybody steer me on the right course?
Thanks!