Karim Belabas on Mon, 01 Apr 2013 16:27:02 +0200


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

Re: function defaults


* Bill Allombert [2013-03-18 16:14]:
> 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

I made this into a FAQ, together with the new (recommended) strictargs = 1 
setting for 2.6.* and above:

  http://pari.math.u-bordeaux1.fr/faq.html#strictarg

Please go on suggesting new features and/or FAQ entries !

Cheers,

    K.B.
--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite Bordeaux 1          Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux1.fr/~belabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux1.fr/  [PARI/GP]
`