help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do you pre-evaluate a function passed to a macro?


From: Barry Margolin
Subject: Re: How do you pre-evaluate a function passed to a macro?
Date: Fri, 13 Nov 2009 12:01:48 -0500
User-agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)

In article <address@hidden>,
 address@hidden (Gregory J. Grubbs) wrote:

> I'm calling a function, wanting to pass nil instead of the empty string
> when the string argument is zero length.  But the function is defined as
> a macro, so the argument gets passed in unevaluated, which fails.  
> 
> How do I force evaluation of the argument before it is passed in?

Use eval:

(eval `(bbdb-search (bbdb-records) 
                    ',(if (> (length myname) 0) myname nil)
                    nil nil))

> 
> --8<---------------cut here---------------start------------->8---
> ;; myname is a string taken from user input
> (bbdb-search (bbdb-records) (if (> (length myname) 0) myname nil) nil nil)
> --8<---------------cut here---------------end--------------->8---
> 
> The above call produces the following backtrace:
> --8<---------------cut here---------------start------------->8---
>   signal(error ("name must be atomic"))
>   error("name must be atomic")
>   (or (stringp name) (symbolp name) (error "name must be atomic"))
>   (let (clauses) (or (stringp name) (symbolp name) (error "name must be 
>   atomic")) (or (stringp company) (symbolp company) (error "company must be 
>   atomic")) (or (stringp net) (symbolp net) (error "net must be atomic")) (or 
>   (stringp notes) (symbolp notes) (error "notes must be atomic")) (or 
>   (stringp phone) (symbolp phone) (error "phone must be atomic")) (if phone 
>   (setq clauses ...)) (if notes (setq clauses ...)) (if name (setq clauses 
>   ...)) (if net (setq clauses ...)) (if company (setq clauses ...)) (\` (let 
>   ... ... ...)))
>   (lambda (records &optional name company net notes phone) "Search RECORDS 
>   for optional arguments NAME, COMPANY, NET, NOTES, PHONE.\nThis macro only 
>   emits code for those things being searched for;\nliteral nils at 
>   compile-time cause no code to be emitted.\n\nIf you want to reverse the 
>   search, bind `bbdb-search-invert' to t." (let (clauses) (or ... ... ...) 
>   (or ... ... ...) (or ... ... ...) (or ... ... ...) (or ... ... ...) (if 
>   phone ...) (if notes ...) (if name ...) (if net ...) (if company ...) (\` 
>   ...)))((bbdb-records) (if (> (length myname) 0) myname nil) nil nil)
>   (bbdb-search (bbdb-records) (if (> ... 0) myname nil) nil nil)
>   eval((bbdb-search (bbdb-records) (if (> ... 0) myname nil) nil nil))
>   eval-expression((bbdb-search (bbdb-records) (if (> ... 0) myname nil) nil 
>   nil) nil)
>   call-interactively(eval-expression nil nil)
> --8<---------------cut here---------------end--------------->8---

-- 
Barry Margolin, address@hidden
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


reply via email to

[Prev in Thread] Current Thread [Next in Thread]