home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.lisp
- Path: sparky!uunet!europa.asd.contel.com!awds.imsd.contel.com!llyene!jato!ufo!Aig.Jpl.Nasa.Gov!charest
- From: charest@Aig.Jpl.Nasa.Gov (Len Charest)
- Subject: Re: Help: apply for macros ???
- Message-ID: <1992Nov19.222419.4597@jpl-devvax.jpl.nasa.gov>
- Sender: usenet@jpl-devvax.jpl.nasa.gov (For NNTP so rrn will be able to post)
- Nntp-Posting-Host: ai-cyclops
- Reply-To: charest@aig.jpl.nasa.gov
- Organization: NASA/Jet Propulsion Laboratory
- References: <2610@bigfoot.first.gmd.de>
- Date: Thu, 19 Nov 1992 22:24:19 GMT
- Lines: 51
-
- In article <2610@bigfoot.first.gmd.de>, wolf@prosun.first.gmd.de (Wolfgang Koehler) writes:
- [ asks if it is possible to APPLY #'F where F is the name of a macro. ]
-
- [[ This question and an appropriate answer should appear in the FAQ list. ]]
-
- The short answer is no. Recall that #'F is shorthand for (FUNCTION F). However, F
- names a macro--not a function or a LAMBDA-expression--so it is an error to
- evaluate (FUNCTION F) (see CLtL2, p 116).* Furthermore, it is not permitted to
- write (APPLY F ...) either since "the global function value of that symbol [F] is
- used (but it is illegal for the symbol to be the name of a macro or special form."
- (CLtL2, p 145)
-
- It is quite possible to work around this problem:
-
- Layered workaround: if you have the source code for the original macro then you
- might want to rewrite it such that the macroexpansion is simply a call to a
- function. The function may passed to APPLY. Here, the only job of the
- macroexpansion is to interpret, quote, rewrite or otherwise massage the arguments
- of the macro. The philosophy behind this approach can be summed up as "If I can
- do it with a macro then I should also be able to do it with an equivalent function
- call." Example:
-
- (defun define-widget (name color viscosity)
- ...code to make a widget...)
-
- ;;; this macro simply quotes its args
- (defmacro defwidget (name &key color viscosity)
- ;;there is an underlying function to do the work
- `(define-widget ',name ',color ',viscosity))
-
- ;;; macroexpansion reveals the equivalent function call:
- (macroexpand-1 '(defwidget jello :color green)
- => (define-widget 'jello 'green nil)
-
- Encapsulation workaround: you could simply define a function that calls your
- macro. One pitfall here is that the macro call will not evaluate its arguments,
- so if the macroexpansion quotes any arguments, an explicit call to EVAL
- around those arguments will be necessary to get the encapsulation to perform
- correctly.
-
- DEFSUBST workaround: you could redefine your macro as a DEFSUBST--that is, a
- function that is proclaimed inline. An inline function *is* a function, so it may
- be used with APPLY, but lexical calls to it will also be expanded inline by the
- compiler.
-
- #' workaround: you could redefine the #' reader to do whatever you want. I don't
- recommend this approach. ;-)
- ..................................................
- Len Charest, Jr.
- JPL Artificial Intelligence Group
- charest@aig.jpl.nasa.gov
-