XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next

&key


Type:   -   keyword
Source:   -   xleval.c

Syntax

&key key-arg ...
&key (key-arg [key-value [supplied-p-var]]) ...
&key ((key-symbol key-arg) [key-value [supplied-p-var]]) ...
key-arg - keyword argument
key-symbol - keyword argument symbol
key-value - keyword argument initialization
supplied-p-var - argument existence variable

Description

In XLISP, there are several times that you define a formal argument list for a body of code [like defun , defmacro , :answer and lambda]. All of the formal arguments that are defined are required to appear in the invocation of the defined function or operation. If there are any &optional arguments defined, they will be filled in order.

There are other optional arguments called 'keyword' arguments. These arguments are not position dependent but can be specified in any order by a preceding keyword [a symbol with a leading colon ':']. If there is no 'key-symbol' specified in the argument list, the keyword will be constructed from the 'key-arg' name by adding a leading colon ':'. For example a 'key-arg' of 'furter' will generate a keyword symbol of ':furter'.

Like the &optional arguments, there can be initialization values provided via the 'key-value' argument. If there is no 'key-value' argument and no value is provided by the function call, the 'key-arg' value will be NIL.

The 'supplied-p-var', if it is specified, will contain a  T  if the 'key-arg' value was supplied by the function call and a NIL if it was not supplied by the function call. This 'supplied-p-var' allows the programmer to test for an argument's existence. At the end of the function or operation execution, these local symbols and their values are are removed.

Examples

(defun foo (a &key b c)
  (print a) (print b) (print c))

(foo)                           ; error: too few arguments
(foo 1)                         ; prints 1 NIL NIL
(foo 1 2)                       ; prints 1 NIL NIL
(foo 1 :b 2 :c 3)               ; prints 1 2 3
(foo 1 :c 3 :b 2)               ; prints 1 2 3
(foo 1 :b 3 :b 2)               ; prints 1 3 NIL

(defun fee (a &key (b 9 b-passed))
  (print a) (print b)
  (if b-passed (print "b was passed")
               (print "b not passed")))

(fee)                           ; error: too few arguments
(fee 1)                         ; prints 1 9 "b not passed"
(fee 1 2)                       ; prints 1 9 "b not passed"
(fee 1 :b 2)                    ; prints 1 2 "b was passed"

(defun fi (a &key ((:mykey b) 9 b-passed))
  (print a) (print b)
  (if b-passed (print "b was passed")
               (print "b not passed")))

(fi)                            ; error: too few arguments
(fi 1)                          ; prints 1 9 "b not passed"
(fi 1 2)                        ; prints 1 9 "b not passed"
(fi 1 :b 2)                     ; prints 1 9 "b not passed"
(fi 1 :mykey 2)                 ; prints 1 2 "b was passed"

Note: There is a '&allow-other-keys' keyword in XLISP and Common Lisp. In the case of XLISP, this keyword is extraneous since the default for keyword arguments is to allow other keys (without errors).

See the &key keyword in the XLISP 2.0 manual.

  Back to Top


XLISP > XLISP 2.0  -  Contents  -  Reference  -  Previous | Next