Skip to content

Question about measurements #518

@tonicebrian

Description

@tonicebrian

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions