I need support for quantities with units. I'd like the type system to enforce unit correctness as much as possible. For example, it shouldn't be possible to combine grams with dollars. I'm going down the path of parameterized types, but this code seems far more repetitious than Scala code I've seen from others.
abstract class UnitOfMeasure
abstract class Range[T] {
type T <: UnitOfMeasure
}
class Quantity[T <: UnitOfMeasure](value: Double)
class DefiniteRange[T<:UnitOfMeasure](lowerBound: Quantity[T], upperBound: Quantity[T]) extends Range[T]
class Confidence(conf: Double) {
require(0.0 <= conf && conf <= 1.0)
}
class ConfidenceInterval[T<:UnitOfMeasure](lowerBound: Quantity[T], upperBound: Quantity[T], confidence: Confidence) extends Range[T] {
def this(lower: Quantity[T], upper: Quantity[T]) = this(lower, upper, new Confidence(.90))
}
Is there a cleaner way to do this? The drumbeat of "T<:UnitOfMeasure" is the main thing that's bothering me.