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

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

Re: around advice: why does (and ad-do-it nil) return t?


From: Kevin Rodgers
Subject: Re: around advice: why does (and ad-do-it nil) return t?
Date: Mon, 13 Feb 2012 21:18:59 -0700
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.26) Gecko/20120129 Thunderbird/3.1.18

On 2/13/12 6:03 AM, Jambunathan K wrote:
Leo Alekseyev<dnquark@gmail.com>  writes:

Consider the following code:

(defun foobar () t)
(defadvice foobar (around foobar-advice activate)
   (and ad-do-it nil))

Evaluating the advised foobar seems to return t.  Why?  Naively, one
expects (and [whatever] nil) to evaluate to nil!

One possible reason could be this:

(info "(elisp) Around-Advice")
,----
|  -- Variable: ad-do-it
|      This is not really a variable, rather a place-holder that looks
|      like a variable.  You use it in around-advice to specify the place
|      to run the function's original definition and other "earlier"
|      around-advice.
`----

May be you are looking for ad-return-value.

(info "(elisp) Defining Advice")
,----
|  -- Variable: ad-return-value
|      While advice is executing, after the function's original
|      definition has been executed, this variable holds its return
|      value, which will ultimately be returned to the caller after
|      finishing all the advice.  After-advice and around-advice can
|      arrange to return some other value by storing it in this variable.
`----

Exactly.  The advised function always returns ad-return-value (which the
various pieces of advice may alter).  As explained in the "Combined Definition"
node of the Elisp manual:

----------------------------------------------------------------------
Suppose that a function has N pieces of before-advice (numbered from 0
through N-1), M pieces of around-advice and K pieces of after-advice.
Assuming no piece of advice is protected, the combined definition
produced to implement the advice for a function looks like this:

     (lambda ARGLIST
       [ [ADVISED-DOCSTRING] [(interactive ...)] ]
       (let (ad-return-value)
         before-0-body-form...
              ....
         before-N-1-body-form...
         around-0-body-form...
            around-1-body-form...
                  ....
               around-M-1-body-form...
                  (setq ad-return-value
                        apply original definition to ARGLIST)
               end-of-around-M-1-body-form...
                  ....
            end-of-around-1-body-form...
         end-of-around-0-body-form...
         after-0-body-form...
               ....
         after-K-1-body-form...
         ad-return-value))
----------------------------------------------------------------------

Basically, ad-do-it expands to

                  (setq ad-return-value
                        apply original definition to ARGLIST)

--
Kevin Rodgers
Denver, Colorado, USA




reply via email to

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