function-keywords
function-keywords Standard Generic Function
Syntax:
function-keywords method → keys, allow-other-keys-p
Method Signatures:
function-keywords (method standard-method)
Arguments and Values:
method—a method.
keys—a list.
allow-other-keys-p—a generalized boolean.
Description:
Returns the keyword parameter specifiers for a method.
Two values are returned: a list of the explicitly named keywords and a generalized boolean that states whether &allow-other-keys had been specified in the method definition.
Examples:
(defmethod gf1 ((a integer) &optional (b 2)
&key (c 3) ((:dee d) 4) e ((eff f)))
(list a b c d e f))
→ #<STANDARD-METHOD GF1 (INTEGER) 36324653>
(find-method #’gf1 ’() (list (find-class ’integer)))
→ #<STANDARD-METHOD GF1 (INTEGER) 36324653>
(function-keywords \*)
→ (:C :DEE :E EFF), *false*
(defmethod gf2 ((a integer))
(list a b c d e f))
→ #<STANDARD-METHOD GF2 (INTEGER) 42701775>
(function-keywords (find-method #’gf1 ’() (list (find-class ’integer))))
→ (), *false*
(defmethod gf3 ((a integer) &key b c d &allow-other-keys)
(list a b c d e f))
(function-keywords \*)
→ (:B :C :D), *true*
Affected By:
defmethodSee Also:
defmethodExpanded Reference: function-keywords
Inspecting Keyword Parameters of a Method
function-keywords returns two values: a list of keyword parameter names accepted by a method, and a boolean indicating whether &allow-other-keys was specified.
(defgeneric make-thing (type &key))
(defmethod make-thing ((type (eql :box)) &key width height depth)
(list :box width height depth))
(let ((m (find-method #'make-thing '() (list (intern-eql-specializer :box)))))
;; Note: intern-eql-specializer is not standard; use find-method differently
)
;; A more portable approach using compute-applicable-methods:
(defgeneric create-item (kind &key))
(defmethod create-item ((kind (eql :circle)) &key radius color)
(list kind radius color))
;; Inspect keywords via a method found through the generic function
(let* ((methods (compute-applicable-methods #'create-item '(:circle)))
(m (first methods)))
(multiple-value-list (function-keywords m)))
=> ((:RADIUS :COLOR) NIL)
Detecting &allow-other-keys
The second return value indicates whether the method accepts arbitrary keyword arguments.
(defgeneric flexible-op (x &key))
(defmethod flexible-op ((x integer) &key start end &allow-other-keys)
(list x start end))
(let* ((methods (compute-applicable-methods #'flexible-op '(42)))
(m (first methods)))
(multiple-value-list (function-keywords m)))
=> ((:START :END) T)
Methods with No Keywords
A method without &key returns empty keyword information.
(defgeneric simple-op (x))
(defmethod simple-op ((x number)) x)
(let* ((methods (compute-applicable-methods #'simple-op '(42)))
(m (first methods)))
(multiple-value-list (function-keywords m)))
=> (NIL NIL)