Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>See <a href="http://www.lispworks.com/documentation/HyperSpec/Body/03_dad.htm">Common Lisp Hyperspec</a> for full details of keyword parameters. Notice that </p> <pre><code>(defun fun (&amp;key key-param) ...) </code></pre> <p>is actually short for:</p> <pre><code>(defun fun (&amp;key ((:key-param key-param)) ) ...) </code></pre> <p>The full syntax of a keyword parameter is:</p> <pre><code>((keyword-name var) default-value supplied-p-var) </code></pre> <p><code>default-value</code> and <code>supplied-p-var</code> are optional. While it's conventional to use a keyword symbol as the <code>keyword-name</code>, it's not required; if you just specify a <code>var</code> instead of <code>(keyword-name var)</code>, it defaults <code>keyword-name</code> to being a symbol in the keyword package with the same name as <code>var</code>.</p> <p>So, for example, you could do:</p> <pre><code>(defun fun2 (&amp;key ((myoption var))) (print var)) </code></pre> <p>and then call it as:</p> <pre><code>(fun 'myoption 3) </code></pre> <p>The way it works internally is when the function is being called, it steps through the argument list, collecting pairs of arguments <code>&lt;label, value&gt;</code>. For each <code>label</code>, it looks in the parameter list for a parameter with that <code>keyword-name</code>, and binds the corresponding <code>var</code> to <code>value</code>.</p> <p>The reason we normally use keywords is because the <code>:</code> prefix stands out. And these variables have been made self-evaluating so that we don't have to quote them as well, i.e. you can write <code>:key-param</code> instead of <code>':key-param</code> (FYI, this latter notation was necessary in earlier Lisp systems, but the CL designers decided it was ugly and redundant). And we don't normally use the ability to specify a keyword with a different name from the variable because it would be confusing. It was done this way for full generality. Also, allowing regular symbols in place of keywords is useful for facilities like CLOS, where argument lists get merged and you want to avoid conflicts -- if you're extending a generic function you can add parameters whose keywords are in your own package and there won't be collisions.</p> <p>The use of keyword arguments when defining packages and exporting variables is again just a convention. <code>DEFPACKAGE</code>, <code>IN-PACKAGE</code>, <code>EXPORT</code>, etc. only care about the names they're given, not what package it's in. You could write</p> <pre><code>(defpackage key-param) </code></pre> <p>and it would usually work just as well. The reason many programmers don't do this is because it interns a symbol in their own package, and this can sometimes cause package conflicts if this happens to have the same name as a symbol they're trying to import from another package. Using keywords divorces these parameters from the application's package, avoiding potential problems like this.</p> <p>The bottom line is: when you're using a symbol and you only care about its name, not its identity, it's often safest to use a keyword.</p> <p>Finally, about distinguishing keywords when they're used in different ways. A keyword is just a symbol. If it's used in a place where the function or macro just expects an ordinary parameter, the value of that parameter will be the symbol. If you're calling a function that has <code>&amp;key</code> arguments, that's the only time they're used as labels to associate arguments with parameters.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload