| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| On Aug 20, 11:55*am, Didier Verna <did...@lrde.epita.fr> wrote: > * * * *Sometimes, I'd like to express some logic or contraints onthe > 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 |
|
#3
| |||
| |||
| > 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/ |
|
#4
| |||
| |||
| On Aug 20, 4:34*pm, Michael Weber <mw+goo...@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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.