fromJust Nothing problem

Donald Bruce Stewart dons at cse.unsw.edu.au
Tue Nov 14 22:43:59 EST 2006


ndmitchell:
> Hi
> 
> >My last impression is that instead of using  -xc  it is better to write
> >programs in a debug-friendly style. For example, let  g x  must
> >return (Just _), but the programmer is not 100% sure that  g x  is free
> >of bugs. Then, instead of
> >                          f x = h $ fromJust $ g x
> >one needs too write
> 
> There are lots of solutions I use, all of which I think are nicer than your 
> one!
> 
> Use a safe module:
> http://neilmitchell.blogspot.com/2006/11/library-idea-safe-library.html
> - always works, a little bit of effort (I have such a module in my
> code). You then get:
> 
> f x = h $ fromJustNote "Foo.f" $ g x

On a similar note, LocH would have:

    $ ./a.out 
    a.out: A.hs:12:20-25: Maybe.fromJust: Nothing

Given:

    01  import Debug.Trace.Location
    02  import qualified Data.Map as M
    03  import Data.Maybe
    04
    05  main = do print f
    06
    07  f = let m = M.fromList
    08                  [(1,"1")
    09                  ,(2,"2")
    10                  ,(3,"3")]
    11          s = M.lookup 4 m
    12      in fromJustSafe assert s
    13
    14  fromJustSafe a s = check a (fromJust s)

So there's lots of solutions now!

-- Don


More information about the Glasgow-haskell-users mailing list