Possibility to invert units automatically
Hi! Sometimes I run into this problem:
>>> 5 min/km -> km/h
error: while type checking
┌─ <input:22>:1:1
│
1 │ 5 min/km -> km/h
│ ^^^^^^^^ -- ^^^^ Velocity
│ │ │
│ │ incompatible dimensions in unit conversion
│ Time / Length
│
= left hand side: Length⁻¹ × Time
right hand side: Length × Time⁻¹ [= Velocity]
Suggested fix: invert the expression on the right hand side
>>> 1 / (5 min/km) -> km/h
1 / (5 minute / kilometre) ➞ kilometre / hour
= 12.0 km/h [Velocity]
In my perfect world, I would just be allowed to convert Length⁻¹ × Time ➞ Length × Time⁻¹. Of course that only make sense in some situations, so it's good that it's not allowed. But maybe this is something that should be configurable? Because in a lot of cases it makes perfect sense. Another big one is fuel economy (miles / gallon -> liters / 100 km, both are widely used).
>>> 5 min/km -> km/h
5 minute / kilometre ➞ kilometre / hour
note: automatic inversion from Length⁻¹ × Time to Length × Time⁻¹
= 12.0 km/h [Velocity]
I like this idea. I think it's feasible with the way units are handled, although I'm not familiar enough to say for sure. It definitely does make sense to me though to add this feature.
I kind of think this should stay an error. Adding "magic" like this is always tempting, but may ultimately lead to a lot of confusion in cases where it goes wrong. Making it configurable is also something that I'd rather not do.
That said, maybe we can improve the error message here and include the result of the inverted conversion in the error message?
I’m thinking maybe a function, possibly an ffi function, and referencing that in the error. That way you could get the value in numbat without as much magic, and won't rely on copy pasting it from error messages if you need it.