iets3.opensource icon indicating copy to clipboard operation
iets3.opensource copied to clipboard

Physunits: Compatibility of units of the same quantity + conversions between them

Open alexanderpann opened this issue 5 months ago • 0 comments

Let's say the user writes: val length: number = 1 km than this is allowed. When implicit conversions are enabled, 1 m would be converted to 1000m and the Type cm would be completely ignored.

As I understand it, units of the same quantity are always compatible, as there must be a conversion between them. At type system level, we only see 2x the quantity length here, so the types are compatible. We could add a checking rule that checks whether there really is an implicit conversion and otherwise displays an error. Implicit conversions (e.g. cm to m) can be currently intentionally deactivated. Otherwise, you can write things like assert 1 km equals 1000 [C] [1 ms], since 1km is automatically converted to 1000 m.

There is also the issue that after the conversion the value might not fit into a type when there is a range: val length: number[0|1] = 1km You cannot easily compare the ranges, you would have to perform the conversion in the type system calculation to determine the new range. What would be possible, however, is to automatically change the type e.g. number[min|max] to real if we know that an implicit conversion is available. This is already done in certain cases any way to avoid rounding losses. In this case, the user would be forced to change the type to real so that the value range is at least correct again.

alexanderpann avatar Sep 26 '24 09:09 alexanderpann