Bill Allombert on Mon, 18 Mar 2013 16:14:25 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: function defaults


On Sat, Mar 09, 2013 at 09:20:57PM -0500, Charles Greathouse wrote:
> In cases like this I just write
> 
> foo(a=-1, b=-1)={
>   if(a==-1 || b==-1, error());
>   \\ code
> }
> 
> where -1, [], etc. is chosen as an argument which could not be valid for
> the function.

You can do even better:

req(x)=error("missing mandatory argument ",x);

foo(a=req("a"), b=req("b"))=
{
  \\code
}

? foo(1)
  ***   at top-level: foo(1)
  ***                 ^------
  ***   in function foo: a=req("a"),b=req("b")
  ***                                 ^--------
  ***   in function req: error("missing manda
  ***                    ^--------------------
  ***   user error: missing mandatory argument b

Cheers,
Bill.