I've been grappling with this issue for the past 24 hours with little success, and already posted a couple of related questions, so apologies if anyone has seen it before. I think what I want to do is conceptually quite simple, and looks like this:
sealed trait DataType { type ElemT <: Numeric[ElemT] } trait PositionsData extends DataType { type ElemT = Double } trait WeightsData extends DataType { type ElemT = Double } trait Error case class TypeDoesntMatch() extends Error case class DataPoint[T <: DataType] ( point: T#ElemT ) { def addToDataPoint(addTo: DataPoint[T]): Either[Error, DataPoint[T]] = Right(DataPoint[T](this.point + addTo.point)) // above method generates the error type mismatch; found: T#ElemT required: String def addToDataPoint(addTo: DataPoint[_]): Either[Error, DataPoint[T]] = Left(TypeDoesntMatch()) } // Example user behaviour - val d1 = DataPoint[PositionsData](1.1) val d2 = DataPoint[PositionsData](2.2) val d3 = DataPoint[WeightsData](3.3) d1.addToDataPoint(d2) // should return Right(DataPoint[PositionsData](3.3)) d3.addToDataPoint(d2) // should return Left(TypeDoesntMatch()) The idea is to let the user of the library choose from a pre-defined list of (numeric) data types (the DataType trait) when creating (say) a DataPoint. The author/maintainer of the library can then set exactly what numeric data type each DataType uses, away from the user's view.
So I'd like to define an enumeration (in the general sense) of DataTypes, each with their own associated numeric type. I'd then like to be able to pass these to the DataPoint case class as a generic. The bits I'm having trouble with are
a) I can't figure out how to constrain ElemT to Numeric types only - the <: Numeric[ElemT] in the code below doesn't work, I think because, as others have pointed out, Double is not a subclass of Numeric[Double].
b) I am having a bit of trouble figuring out how to introduce the necessary implicit conversion so that Numeric.plus is used in this.point + addTo.point.
I'm not at all fussy about how I achieve this and if my approach looks totally wrong then I'd be happy to be told that. Sincere thanks to anyone who can help me escape this on-going type-mare.
type ElemT <: Numeric[ElemT], which isn't bad in and of itself, but it can't be overridden with a simple type:type ElemT = Double. Then it looks like you want to allow something likeDataPoint[WeightsData](n)but only wherenis the numeric type pre-selected forWeightsData. It would be clearer if you included example code as requested. Don't describe, demonstrate. Add examples instead of explanations.