Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#9816 closed feature request (fixed)

Add function for size-checked conversion of Integral types

Reported by: spl Owned by:
Priority: normal Milestone: 7.10.1
Component: Core Libraries Version: 7.9
Keywords: report-impact Cc: thoughtpolice, hvr, ekmett, core-libraries-committee@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): D512
Wiki Page:

Description (last modified by spl)

Based on the discussion in this thread, I would like to add a function to the base library that is similar to fromIntegral but only successful if the argument fits in the result type.

If possible, I would like to get this into 7.10. My apologies for running late on it. Hopefully, since it is a relatively small change overall, the "only" controversy will be bikeshedding.

I have concluded that adding @hvr's intCastMaybe from int-cast is the best possible option. Previously, I thought a Bounded-based version was also useful; however, I realized that it did not deal optimally with conversions like Int<->Word/Int8<->Word8/etc. as well as intCastMaybe does and would need specialized versions that intCastMaybe provides automatically.

Change History (7)

comment:1 Changed 5 years ago by spl

Phab diff coming momentarily, I hope.

comment:2 Changed 5 years ago by spl

Differential Rev(s): D512
Status: newpatch

comment:3 Changed 5 years ago by simonpj

Cc: core-libraries-committee@… added
Component: libraries/baseCore Libraries

We need a ruling from the Core Libraries Committee.

Simon

comment:4 in reply to:  3 Changed 5 years ago by spl

Description: modified (diff)

comment:5 Changed 5 years ago by Herbert Valerio Riedel <hvr@…>

In 02f8f6ad7bd3d792459a1d33e8d0d57dcf1ea424/ghc:

Add function for size-checked conversion of Integral types

The new function `Data.Bits.toIntegralSized` provides a similar
functionality to `fromIntegral` but adds validation that the
argument fits in the result type's size.

The implementation of `toIntegralSized` has been derived from `intCastMaybe`
(which is part of Herbert Valerio Riedel's `int-cast` package,
see http://hackage.haskell.org/package/int-cast)

Addresses #9816

Reviewed By: ekmett, austin

Differential Revision: https://phabricator.haskell.org/D512

comment:6 Changed 5 years ago by thoughtpolice

Milestone: 7.10.1
Resolution: fixed
Status: patchclosed

Merged, thanks!

comment:7 Changed 5 years ago by hvr

Keywords: report-impact added
Note: See TracTickets for help on using tickets.