Page 1 of 1

Passing &key arguments through to lower levels

Posted: 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?

Re: Passing &key arguments through to lower levels

Posted: Sun Jan 29, 2012 12:05 am
by ramarren
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.

Re: Passing &key arguments through to lower levels

Posted: Wed Feb 01, 2012 11:16 am
Thank you. I will remember the code tags in the future.