On Tue, 7 Apr 2009, Max Rabkin wrote:

The problem with your code is that the type of maxBound is
unspecified. You need (maxBound `asTypeOf` i), or enable
ScopedTypeVariables and use (maxBound :: a) (I think).

i is Integer, so asTypeOf is not so easy to apply. I propose to write


safeFromIntegerAux :: (Num a, Integral a) =>
   a -> Integer -> Maybe a
safeFromIntegerAux mb i =
   if i > toInteger mb
     then Nothing
     else Just (fromInteger i)

safeFromInteger = safeFromIntegerAux maxBound


Btw. this is another instance where my beloved toMaybe function is useful. :-)
  
http://hackage.haskell.org/packages/archive/utility-ht/0.0.4/doc/html/Data-Maybe-HT.html#v%3AtoMaybe
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to