icu4x icon indicating copy to clipboard operation
icu4x copied to clipboard

FixedDecimal `*ed` methods should be infallible

Open sffc opened this issue 1 year ago • 5 comments
trafficstars

This function is misleading:

pub fn concatenated_end(self, other: FixedDecimal) -> Result<Self, FixedDecimal>

In the error case, it returns other, but it still throws away self. It should probably return a tuple containing both FixedDecimals. It makes it tempting to write code such as mantissa.concatenated_end(fraction).unwrap_or_else(|fd| fd), which is wrong because it promotes fraction when it should probably keep mantissa and throw away fraction.

I think we should delete this function since it is only sugar over concatenate_end, which is harder to use incorrectly.

sffc avatar May 02 '24 00:05 sffc

For the datetime use case, I think it would be useful to have a FixedDecimal constructor that accepts usize, usize as the integer and fractional parts. Then we avoid this infallibility altogether.

robertbastian avatar May 02 '24 07:05 robertbastian

For the datetime use case, I think it would be useful to have a FixedDecimal constructor that accepts usize, usize as the integer and fractional parts. Then we avoid this infallibility altogether.

I don't think it solves the overlap issue.

sffc avatar May 02 '24 08:05 sffc

I think it does. It would use IntIterator::from(integer).chain(IntIterator::from(fractional)) and then shift the resulting FixedDecimal by fractional.log10().

robertbastian avatar May 02 '24 09:05 robertbastian

Hmm, that's quite clever. How does it handle these cases though?

  • Seconds = 123, Nanoseconds = 20 (needs to know that there are leading zeros: easy enough)
  • Seconds = 123, Nanoseconds = 4444444444 (same problem as we currently have: overlapping fields)

sffc avatar May 03 '24 22:05 sffc

I wouldn't be opposed to implementing a plus operator that just performs a sum.

sffc avatar May 03 '24 22:05 sffc