Introducing NewType
s for units
#6
Replies: 4 comments
-
FYI @thomas-nicolai-frequenz @matthias-wende-frequenz @tiyash-basu-frequenz @sahas-subramanian-frequenz @ela-kotulska-frequenz @jakub-toptal |
Beta Was this translation helpful? Give feedback.
-
We discussed it and (sort of) agreed having units encoded in the type system would be a good idea, but it might be hard to get right (not trivial to implement), and there is no urgency now, so we are delaying it. For now we only make sure units are present in the documentation and we don't use variable suffixes, as it will be a breaking change now, and in the future if we implement units as types. At some point we can experiment implementing this and make a final decision if the types make sense or not, and if it doesn't we can make a breaking change only once to add the suffixes, if we still think they are worthy. |
Beta Was this translation helpful? Give feedback.
-
for the record, the ringbuffer class already uses |
Beta Was this translation helpful? Give feedback.
-
This was done a long time ago. Now in a separate repo: https://github.com/frequenz-floss/frequenz-quantities-python/ |
Beta Was this translation helpful? Give feedback.
-
Think if introducing new types for units (
Power
,Current
,Voltage
,Energy
, etc) makes sense.Random examples:
Pros: avoid bugs by combining different units (
power + current = ???
) or combining different multipliers accidentally (watt + kilowatt
when you wanted to dowatt + watt
). Also we could have conversion methods. Likedatetime
that you can create it from a timestamp, or from specifying a year, month, date, etc.Cons: we probably need to overload operators to set which kind of operations are allowed and what are the resulting units (
current * voltage = power
,power * time = energy
), which can be hard to do in a complete way.Alternatives: https://pint.readthedocs.io/en/stable/ (compatible with
numpy
)If this proves to be too complicated to implement, we should at least establish a convention of prefixes, so if some attribute is in Watt, it should be called
something_w
, same for kW, A, kWh, etc.:something_kw
,something_a
,something_kwh
, ...Status
We discussed it and (sort of) agreed having units encoded in the type system would be a good idea, but it might be hard to get right (not trivial to implement), and there is no urgency now, so we are delaying it.
For now we only make sure units are present in the documentation and we don't use variable suffixes, as it will be a breaking change now, and in the future if we implement units as types.
At some point we can experiment implementing this and make a final decision if the types make sense or not, and if it doesn't we can make a breaking change only once to add the suffixes, if we still think they are worthy.
Beta Was this translation helpful? Give feedback.
All reactions