Explore units of measure in Scala as a way to keep physical quantities, currencies, rates, and domain-specific numeric values type-safe rather than treating all numbers as interchangeable.
Units of measure: A modeling technique that prevents dimensionally different quantities from being treated as the same kind of number.
Plain numeric types are convenient, but they are often too weak for domains that care about physical quantities, currencies, rates, or time-based measures. If meters, seconds, dollars, and percentages are all just Double, the compiler cannot protect you from mixing them incorrectly.
Most unit bugs come from code that looks numerically valid but is domain-invalid:
Units of measure reduce this class of mistake by making the quantity type carry more semantic information.
Scala teams often reach for libraries such as Squants when they need many standard physical units and conversions. That can be a good fit for:
A smaller domain may do better with lightweight wrappers, opaque types, or value classes if only a few quantities matter and the team wants less library surface.
Units of measure are not just about arithmetic correctness. They also improve API design:
This is especially useful in Scala because strong typing is already part of the language’s value proposition.
If the problem is not really dimensional correctness, additional quantity types can add ceremony without much value.
The type may distinguish units, but the system still hides when and where conversion is allowed.
The code falls back to raw numeric helpers frequently enough that the type-safe unit model loses much of its protective value.
Use units of measure where the domain has real quantity semantics and mistakes would be expensive or subtle. Prefer explicit conversions, keep quantity types near the domain vocabulary, and avoid wrapping raw numbers just because the type system can.