[Haskell-cafe] Trying to write 'safeFromInteger'

Edward Kmett ekmett at gmail.com
Wed Apr 8 08:09:55 EDT 2009


Of course your safeFromInteger isn't really safe, what about the minBound?
=)

On Tue, Apr 7, 2009 at 5:27 PM, Kannan Goundan <kannan at cakoose.com> wrote:

> Here's my code (in file "Test.hs")
>
>  safeFromInteger :: (Num a, Integral a, Bounded a) => Integer -> Maybe a
>  safeFromInteger i =
>    if i > (toInteger maxBound)
>      then Nothing
>      else Just (fromInteger i)
>
> Here's the error from GHCi 6.10.1:
>
>  Test.hs:3:19:
>    Ambiguous type variable `a' in the constraints:
>      `Bounded a' arising from a use of `maxBound' at Test.hs:3:19-26
>      `Integral a' arising from a use of `toInteger' at Test.hs:3:9-26
>    Probable fix: add a type signature that fixes these type variable(s)
>
> It's almost like GHC thinks that the type variable "a" can't be part of the
> "Bounded" lass and the "Integeral" class.
>
> I plan to use "safeFromInteger" for converting from Word8, Word16, Word32,
> etc.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090408/87619c10/attachment.htm


More information about the Haskell-Cafe mailing list