Proposal: die to System.Exit (and/or Prelude)

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Sat Dec 14 13:24:48 UTC 2013


On 14 December 2013 22:19, Roman Cheplyaka <roma at ro-che.info> wrote:
> I agree that this function would be useful for quick&dirty prototyping.
>
> However, I'm not sure we should encourage its use. What looks to me like
> a superior approach is to throw proper exceptions.

I've written something similar to this die function specifically to
deal with these exceptions (or to exit early out of main if I know
nothing more can proceed) rather than relying upon GHC to deal with
them.

That said, I'm not sure if it's worth adding; +0.5 to having it in System.Exit.

>
> Here's an example in my own code:
> https://github.com/haskell-suite/haskell-names/blob/master/hs-gen-iface/src/hs-gen-iface.hs#L31
>
> The main advantage of this is that it's much easier to turn the 'main'
> code into library code, which is not supposed to write to stderr
> anymore. Exceptions carry important semantic information about what
> actually happened, and can be caught and handled if needed. (The
> ExitCode exception can also be caught, but it doesn't tell us anything,
> and the output would have already gone to stderr.)
>
> As a bonus, this approach forces you to separate (in code) message
> strings from places where you die, which in my experience leads to much
> cleaner code.
>
> There's a caveat that the standard doesn't specify what happens to
> uncaught exceptions, so we have to rely on the runtime doing the right
> thing for us. Well, GHC's one does.
>
> Roman
>
> * Simon Hengel <sol at typeful.net> [2013-12-14 11:56:11+0100]
>> Hi!
>> I propose to add
>>
>>     die :: String -> IO ()
>>     die err = hPutStrLn stderr err >> exitFailure
>>
>> to System.Exit.
>>
>> Reasoning:
>>
>> (1) It's a frequently required functionality.  Almost every command-line
>>     program has code similar to this.
>>
>> (2) The definition is relatively short, but in addition to the
>>     definition, you need two import statements.
>>
>> (3) It's frequently done wrong (e.g. writing to stdout instead of
>>     stderr, or not using exitFailure, or both).
>>
>> I haven't done any extensive research on Hackage, but I quickly looked
>> at Haddock.  Here we have a definition of die [1], but we also print to
>> stdout at a couple of place and then call exitFailure [2].
>>
>> Personally, I think it should be re-exported from Prelude.  But this may
>> be controversial.  So let's have two separate votes:
>>
>> Add System.Exit.die:            +1
>> Re-export it from Prelude:      +1
>>
>> (discussion until December, 28th)
>>
>> Cheers,
>> Simon
>>
>> [1] https://github.com/ghc/haddock/blob/8d4c94ca5a969a5ebbb791939fb0195dc672429e/src/Haddock/Utils.hs#L303
>> [2] https://github.com/ghc/haddock/blob/c6faeae064668125721b0d5e60f067f90c538933/src/Haddock.hs#L87
>> _______________________________________________
>> Libraries mailing list
>> Libraries at haskell.org
>> http://www.haskell.org/mailman/listinfo/libraries
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>



-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
http://IvanMiljenovic.wordpress.com


More information about the Libraries mailing list