From: Didier Verna
Subject: Expressing some logic in the use of keyword parameters
Date: 
Message-ID: <muxwsibadg8.fsf@uzeb.lrde.epita.fr>
       Sometimes, I'd like to express some logic or contraints on the
keyword parameters of a function; for instance, that key1 and key2 are
mutually exclusive (they shouldn't be used at the same time when the
function is called).

Are there any libraries that do that sort of thing ?

Thanks.

-- 
Resistance is futile. You will be jazzimilated.

Scientific site:   http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bic�tre, France
Tel. +33 (0)1 44 08 01 85       Fax. +33 (0)1 53 14 59 22

From: Marco Antoniotti
Subject: Re: Expressing some logic in the use of keyword parameters
Date: 
Message-ID: <94794b38-b3c0-4bea-b1ab-e41621e03e7d@m36g2000hse.googlegroups.com>
On Aug 20, 11:55 am, Didier Verna <······@lrde.epita.fr> wrote:
>        Sometimes, I'd like to express some logic or contraints on the
> keyword parameters of a function; for instance, that key1 and key2 are
> mutually exclusive (they shouldn't be used at the same time when the
> function is called).
>
> Are there any libraries that do that sort of thing ?

When I have to do something like that, I just resort to using the
supplied-p parameters

(defun foo (&key (k1 42 k1-supplied-p) (k2 3 k2-supplied-p))
   (when (and k1-supplied-p k2-supplied-p)
      (error "Foo K1 K2.")))

I suppose you could make up a macro for this.

Cheers
--
Marco
From: Michael Weber
Subject: Re: Expressing some logic in the use of keyword parameters
Date: 
Message-ID: <31de758b-26b2-4a10-84f6-4c66002529ff@79g2000hsk.googlegroups.com>
> When I have to do something like that, I just resort to using the
> supplied-p parameters

FWIW, I don't like supplied-p parameters much. It is harder to write
wrappers for functions using them, like this:

(defun wrap-foo (x y &rest args &key k1 k2 &allow-other-keys)
  ;; extra stuff, mangling args, etc. goes here
  (apply #'foo x y args))

If at all possible, I try to arrange it that NIL denotes the default
for keyword parameters.

I.e., instead of
    (defun foo (&key (x 'default))
       ...)

I would rather write
    (defun foo (&key x)
      (setf x (or x 'default))
      ...)

M/
From: Marco Antoniotti
Subject: Re: Expressing some logic in the use of keyword parameters
Date: 
Message-ID: <0286564c-c275-4d18-b183-bd3915bf9fa5@b1g2000hsg.googlegroups.com>
On Aug 20, 4:34 pm, Michael Weber <·········@foldr.org> wrote:
> > When I have to do something like that, I just resort to using the
> > supplied-p parameters
>
> FWIW, I don't like supplied-p parameters much. It is harder to write
> wrappers for functions using them, like this:
>
> (defun wrap-foo (x y &rest args &key k1 k2 &allow-other-keys)
>   ;; extra stuff, mangling args, etc. goes here
>   (apply #'foo x y args))
>
> If at all possible, I try to arrange it that NIL denotes the default
> for keyword parameters.
>
> I.e., instead of
>     (defun foo (&key (x 'default))
>        ...)
>
> I would rather write
>     (defun foo (&key x)
>       (setf x (or x 'default))
>       ...)
>
> M/

I found that it really depends on the actual need of the function.
Somethime it does not make sense to pass NIL as a default.  E.g., a
function traversing an enumerable data structure will usually have
a :START parameter initially set to 0 (and an :END one set to NIL).
So, in this case you you either have to test for 0 (on a parameter
which you could declare as (MOD 42) instead of (OR NULL (MOD 42))) or
you have to resort to the supplied-p parameter.

Sometimes you do have problems wrapping functions, but in the cse you
report you are actually home free.

CL-USER 1 > (defun foo (&rest k-args &key k1 k2 &allow-other-keys)
              k-args)
FOO

CL-USER 2 > (foo :zot 42)
(:ZOT 42)

CL-USER 3 > (defun wrap-foo (&rest k-args &key k1 k2 &allow-other-
keys)
              (apply 'foo k-args))
WRAP-FOO


CL-USER 4 > (wrap-foo :zot 42)
(:ZOT 42)


Cheers
--
Marco