serokell / o-clock

:hourglass: Type-safe time units in Haskell
Mozilla Public License 2.0
49 stars 6 forks source link

Avoid accidental flooring when converting `Time unit` to numeric types #124

Closed dcastro closed 2 years ago

dcastro commented 3 years ago

I found it unexpected that toNum floors the number before converting it to Num a. This is neither documented nor (IMO) something most users would expect to happen.

toNum @Second @Double (sec 1.4)
1.0
toNum @Second @Rational (sec 1.4)
1 % 1

This can lead to surprising results:

λ> toUnit @Second $ ms 999
999/1000s
λ> toNum @Second @Rational $ toUnit @Second $ ms 999
0 % 1

AFAICT, there is no way to convert Time unit to an arbitrary Num a => a without flooring the ratio first.

So, I propose we deprecate toNum (and eventually delete it) and split it into floorRat (which already exists) and toFractional:

dcastro commented 2 years ago

Re-opening this issue.

We've deprecated toNum in v1.3.0. Let's delete it in the next release.