-
-
Notifications
You must be signed in to change notification settings - Fork 123
Open
Description
Sorry if this is not the place but I haven't found a community for asking this question.
My use case is that I have "measurements" that could be either "units", "kg" or "m2" for items, so I have the tuple (item, measurement).
I haven't found a clean way of holding this other than creating a sum type called Measurement and having:
sealed trait Measurement {
val quantity: Quantity[_]
def unfold: (Double, String)
def getNormalizedValue: Double = this.unfold._1
}
case class UnitMeasurement private (quantity: Dimensionless) extends Measurement {
override def unfold: (Double, String) = quantity toTuple Each
}
case class AreaMeasurement private (quantity: Area) extends Measurement {
override def unfold: (Double, String) = quantity.toTuple(SquareMeters)
}
case class MassMeasurement private (quantity: Mass) extends Measurement {
override def unfold: (Double, String) = quantity toTuple Kilograms
}
object Measurement {
def apply(quantity: Double, unit: String): Measurement = unit match {
case "kg" => MassMeasurement(Kilograms(quantity))
case "m²" => AreaMeasurement(SquareMeters(quantity))
case "ea" => UnitMeasurement(Each(quantity))
case _ => throw new IllegalArgumentException(s"Unknown unit: $unit")
}
}
But now I want to have a MeasurementDimension in my anticorruption layer and I'm facing the same problem, creating a new sum type. All this boilerplate looks like I haven't understood properly the type hiearchy of squants.
Am I overengineering? What would be a cleaner approach to work seamlessly with just 3 disparate quantities?
Metadata
Metadata
Assignees
Labels
No labels