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

Niklas Hambüchen mail at nh2.me
Sat Dec 14 12:49:59 UTC 2013


I also think that people should generally use exceptions which I can
catch by names (as opposed to error), especially in libraries, but in
simple top-level cases as you described, your `die` makes a lot of sense
to me. "Easy things should be easy", and removing another case for
`error` sounds like a good thing.

  Add System.Exit.die:     +1

I'd prefer your simple, first version though, since `getProgramName`
might not always print the thing you would like. Glad to be convinced
otherwise though (how do unix tools do this? Is it hardcoded or do they
check their program name)?

  Re-export it from Prelude:  -1

(At least for now, let's have it around firstr.)


On 14/12/13 11:54, Simon Hengel 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.
>>
>> 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.
> 
> For my own code, I actually prefer to stick with Maybe/Either instead of
> exceptions when ever possible.  I'd use `die` only in a top-level
> wrapper, e.g. like so:
> 
>     import Acme.Omitted
> 
>     main :: IO ()
>     main = getContents >>= either die run . parseInput
> 
>     run :: UserInput -> IO ()
>     run = (...)
> 
>     parseInput :: String -> Either String UserInput
>     parseInput = (...)
> 
> Cheers.
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
> 


More information about the Libraries mailing list