Proposal to deprecate and then drop fromJust

David Feuer david.feuer at gmail.com
Tue Feb 24 06:20:18 UTC 2015


I think instead we should get rid of the error message and make
`fromJust` perform an unsafe memory reference.

data IM b a where
  N :: IM 'False a
  J :: a -> IM 'True a

fromJ :: IM 'True a -> a
fromJ (J a) = a

fromJust :: Maybe a -> a
fromJust m = fromJ (unsafeCoerce m)

On Tue, Feb 24, 2015 at 1:08 AM, Erik de Castro Lopo
<mle+hs at mega-nerd.com> wrote:
> Ivan Lazar Miljenovic wrote:
>
>> I've used fromJust in the past when I knew the value would be Just by
>> construction.
>
> I have too. And then a less experienced haskell dev saw that usage in code
> that I had written and used it elsewhere without the same assurances. I
> didn't notice and the Yesod webapp was deployed to production and two
> days later we had a report of "Exception: Maybe.fromJust: Nothing" showing
> up in web pages. Embarassing to say the least. We removed all usage of
> fromJust.
>
> However, if *i* had used fromMaybe to begin with its *highly* likely that
> my colleague would have followed my lead and highly likely he would have
> *thought* about the Nothing case.
>
>> That said, I could always have written my own fromJust function with a
>> custom error message, and did switch some of them to direct pattern
>> matches (so that at least the error message said *where* the error
>> occurred).
>
> Exactly. This:
>
>    fromMaybe (error "Module.functionName : unexpected Nothing") x
>
> is hugely preferable to fromJust.
>
> Erik
> --
> ----------------------------------------------------------------------
> Erik de Castro Lopo
> http://www.mega-nerd.com/
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


More information about the Libraries mailing list