Passing &key arguments through to lower levels

Discussion of Common Lisp
Post Reply
[email protected]
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm

Passing &key arguments through to lower levels

Post by [email protected] » Sat Jan 28, 2012 9:43 pm

I want to extend adjoin, but want to maintain it full flexibility. So somehow I need to pass any keyword arguments that come in through to a call to adjoin I make in my version:

Code: Select all

(defun my-adjoin (item list &key key test test-not)
     (do-something 
            (adjoin item list WHAT-DO-I-PUT-HERE?)))
Or is this putting me into macro territory?
Last edited by ramarren on Sat Jan 28, 2012 11:59 pm, edited 1 time in total.
Reason: Added code tags

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Passing &key arguments through to lower levels

Post by ramarren » Sun Jan 29, 2012 12:05 am

Please use code tags for posting code samples in the future.

Keyword arguments always have a default value, which, if not specified in the argument list, is NIL, which means you can just call ADJOIN with all the arguments.

Code: Select all

(defun my-adjoin (item list &key key test test-not)
  (do-something
      (adjoin item list :key key :test test :test-not test-not)))
If you want to avoid typing you can do something like:

Code: Select all

(defun my-adjoin (item list &rest key-args &key key test test-not)
  (do-something
      (apply #'adjoin item list key-args)))
Common Lisp allows both &rest and &key argument specification. In this particular case the &key would only serve as documentation anyway. But it complicates the interface, and in such cases I usually would prefer to write out the keyword arguments explicitly.

[email protected]
Posts: 20
Joined: Sun Nov 28, 2010 10:34 pm

Re: Passing &key arguments through to lower levels

Post by [email protected] » Wed Feb 01, 2012 11:16 am

Thank you. I will remember the code tags in the future.

Post Reply