Unfortunately the toXxx()
convention doesn’t work well for this problem. Units are not a small, closed set that translate well to a set of functions. That means you’d have an explosion of very specialized functions to cover the wide range of units. Not to mention complex units like Newtons. Moreover, Units like Newton can be represented by a wide range of combinations. So the typealias for it would be something like:
typealias Newtons = UnitsRatio<UnitsProduct<Mass, Length>, UnitsProduct<Time, Time>>
And conversions would need to support any way of representing a Newton, say: kilograms * miles / (hours * minutes)
. This is a big reason why the framework does not provide specialized conversion methods.
Also, notice that the as
and in
methods take instances of Units
and not the class itself. Saying value 'as' Newton
does not make sense because you can’t convert between Unit types, only between unit instances. So Newton
here is a type, but there are many ways to represent it that do not require new classes.
Bespoke conversions are not defined in the framework because of these differences. You can always add them for specific Measure<T>
s as extension methods if you have a set that are particularly important for your use-case. But I’m not sure how much easier, or readable that is vs the current approach.