From: Richard Levitte
Subject: &KEY question
Date: 
Message-ID: <LEVITTE.93Nov28183651@eliza.e.kth.se>
It is possible to define a keyword in a lambda-list like this:

	(defun blah (&KEY ((foo bar) 'default)))

When you call blah, you have to make the call liek this:

(1)	(blah 'foo 1)

What I want to know is if is possible to do this:

(2)	(setq my-keyword 'foo)
	(blah my-keyword 1)	; Is this equivalent to (blah 'foo 1)

I want to know, because I am trying to build a compiler (just for the
fun of it), and I would prefer if the way (2) was disalowed.  It would
oh so much simpler if the keyword has to be resolved at compile-time.

(Now, whoever at Franz Inc., please don't send me your broschure about
Allegro CL.  I have already gotten two of them from you, all alike)

--
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
! Richard Levitte, VMS GNU Emacs hacker   ! tel: int+46-8-18 30 99            !
! Sulv"agen 57, II                        ! fax: none for the moment          !
! S-126 40 H"agersten                     ! Internet: ·······@e.kth.se        !
! SWEDEN                                  !                                   !
!-----------------------------------------------------------------------------!

From: Scott Anderson
Subject: Re: &KEY question
Date: 
Message-ID: <2db0hkINN1ll@ymir.cs.umass.edu>
In article <·····················@eliza.e.kth.se> ·······@e.kth.se 
(Richard Levitte) writes:
>It is possible to define a keyword in a lambda-list like this:
>
>	(defun blah (&KEY ((foo bar) 'default)))
>
>When you call blah, you have to make the call like this:
>
>(1)	(blah 'foo 1)
>
>What I want to know is if is possible to do this:
>
>(2)	(setq my-keyword 'foo)
>	(blah my-keyword 1)	; Is this equivalent to (blah 'foo 1)
>
>I want to know, because I am trying to build a compiler (just for the
>fun of it), and I would prefer if the way (2) was disallowed.  It would be
>oh so much simpler if the keyword has to be resolved at compile-time.
>
>!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
>! Richard Levitte, VMS GNU Emacs hacker   ! tel: int+46-8-18 30 99          
>! Sulv"agen 57, II                        ! fax: none for the moment        
>! S-126 40 H"agersten                     ! Internet: ·······@e.kth.se      
>! SWEDEN                                  !                                 
>!---------------------------------------------------------------------------

The subject is covered on page 79 of CLtL2, which you've obviously
read, since you know this usual syntax.  The relevant example is:

(defun wager (&key ((secret password) nil) amount) 
  (format nil "You ~A $~D"
     (if (eq password 'joe-sent-me) "win" "lose") amount))

(wager :amount 100 'secret 'joe-sent-me) => "You win $100"

Note that the "keyword" argument markers are quoted.  The ":amount"
doesn't neet to be explicitly quoted because keywords are defined to
be self-evaluating, so they always act as if quoted.  The weird
argument, "secret," needs to be quoted or the value of that symbol
would be used, so the example shows that the following would also
work:

(let ((foo 'secret))
  (wage :amount 100 foo 'joe-sent-me)) => "You win $100"

I'm sorry that this makes things hard for your compiler, but it is
legal Common Lisp.  Perhaps you can analyze and optimize the common
case, and punt to the evaluator for these rare cases, rather than
disallowing them..

Scott D. Anderson
········@cs.umass.edu
From: Barry Margolin
Subject: Re: &KEY question
Date: 
Message-ID: <2ddvfaINNisr@early-bird.think.com>
In article <·····················@eliza.e.kth.se> ·······@e.kth.se (Richard Levitte) writes:
>What I want to know is if is possible to do this:
>
>(2)	(setq my-keyword 'foo)
>	(blah my-keyword 1)	; Is this equivalent to (blah 'foo 1)
>
>I want to know, because I am trying to build a compiler (just for the
>fun of it), and I would prefer if the way (2) was disalowed.  It would
>oh so much simpler if the keyword has to be resolved at compile-time.

You have to be able to do run-time keyword resolution in order to handle
APPLY:

(apply #'blah my-arglist)

In fact, I suspect that APPLY is likely to be used quite frequently with
keyworded functions.  It's common for one function to accept keyword
arguments that are simply passed along to another function:

(defun open-foo (&rest open-options)
  (apply #'open "foo" open-options))

If you're trying to optimize the calls at compile time, create two
entrypoints into keyworded functions.  The first entrypoint would parse the
arguments and then fall through to the second entrypoint, which expects the
arguments to be preparsed.  When the compiler sees a call in which all the
keywords are constants it can resolve everything and generate a call to the
second entrypoint.

Note also that unless the function is declared INLINE, it can be redefined
at run-time with different keyword options.  Some assumptions are allowed
when calling between functions in the same compilation unit, but calls from
different files generally have to go through the general interface.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar