[Nix-dev] Proposal: tryCatch function

Andres Loeh andres.loeh at googlemail.com
Tue Jan 6 20:50:06 CET 2009


Hi again, Nicolas.

Sorry, I overlooked this mail. So what I just sent a minute ago was
without having
read your reply to Eelco's mail.

>> Can you give some example use cases of where this would help?  "throw" already
>> takes a string argument, so the error message can be as helpful as you want it
>> to be.  If the "throw" error message cannot show a helpful message because it
>> lacks context (e.g. deep in some function call), the necessary context could be
>> passed in as a string argument, for instance.
>
> Ok, in merge function used for options there is an extra argument.
> This extra argument is the name of the attribute which is computed by
> the merge function.  The name is used to report the location inside
> error messages.  If the input of an attribute is incorrect, you would
> like to know which attribute cause the failure. (functions:
> mergeDefaultOption, mergeTypedOption, .. user defined merge functions
> ..)  The name of the attribute is only used for reporting the location
> of the error, so whatever you want to do you have to accept an extra
> argument which gives you the location of the error.  This extra
> argument is not correct IMHO because it add more complexity to a
> function only to handle something which is not related to the goal of
> the function.  The goal of a merge function is to merge (so one list
> argument) and not to handle errors.
>
> mergeSomething = list:
>  try
>    doTheMerging list
>  catch
>    throw "cannot merge.";
>
> Passing the string argument just add more complexity for nothing.

This is a typical example of a function that shouldn't throw an exception
at all. Rather, it should return something of an option type, and the caller
should have the responsibility to deal with failure.

While this is possible in Nix, it's not terribly convenient, and certainly not
the style we have been using so far. So I'm prepared to be overruled.

Interestingly, a variation of the theme is for functions that can fail to take
some form of failure continuation, i.e., an additional argument that can be
used in case of failure. Passing a string is a crippled form of a failure
continuation, but in that sense it does make sense to pass a string to a
function such as merge.

Cheers,
  Andres



More information about the nix-dev mailing list