unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* around advice: why does (and ad-do-it nil) return t?
@ 2012-02-13 12:43 Leo Alekseyev
  2012-02-13 13:03 ` Jambunathan K
  0 siblings, 1 reply; 3+ messages in thread
From: Leo Alekseyev @ 2012-02-13 12:43 UTC (permalink / raw)
  To: help-gnu-emacs

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!



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: around advice: why does (and ad-do-it nil) return t?
  2012-02-13 12:43 around advice: why does (and ad-do-it nil) return t? Leo Alekseyev
@ 2012-02-13 13:03 ` Jambunathan K
  2012-02-14  4:18   ` Kevin Rodgers
  0 siblings, 1 reply; 3+ messages in thread
From: Jambunathan K @ 2012-02-13 13:03 UTC (permalink / raw)
  To: Leo Alekseyev; +Cc: help-gnu-emacs

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.
`----
-- 



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: around advice: why does (and ad-do-it nil) return t?
  2012-02-13 13:03 ` Jambunathan K
@ 2012-02-14  4:18   ` Kevin Rodgers
  0 siblings, 0 replies; 3+ messages in thread
From: Kevin Rodgers @ 2012-02-14  4:18 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-02-14  4:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-13 12:43 around advice: why does (and ad-do-it nil) return t? Leo Alekseyev
2012-02-13 13:03 ` Jambunathan K
2012-02-14  4:18   ` Kevin Rodgers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).