[Top][All Lists]

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

Re: replacing a function with another one

From: Stefan Monnier
Subject: Re: replacing a function with another one
Date: Wed, 12 Mar 2014 14:26:43 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

> That line doesn´t tell me anything, it only confuses me.

Probably because you don't understand (lambda ...) yet.

> (defun replacement-fn (original-fn-arg0 original-fn-arg1)
>   (foobar)
>   (original-fn original-fn-arg0 original-fn-arg1)
>   (barfoo))

> (callinstead original-fn replacement-fn)

That's pretty much exactly what advice-add does for :around, except that
the original-fn is provided as an additional argument, so you have to write:

   (defun replacement-fn (orig-fun original-fn-arg0 original-fn-arg1)
     (funcall orig-fun original-fn-arg0 original-fn-arg1)
   (advice-add 'original-fn :around 'replacement-fn)

> That would be clear and simple, assuming that calling the original
> function from within a function that is defined to be called instead of
> it always automatically calls the original function.

That's one of the reasons why the original function is passed as an
extra argument.  This way you can choose to either call (original-fn ...)
which will go through the advice recursively, or to call (funcall
orig-fun ...) which will call the unadvised version.

Magically making `original-fn' detect when it's called from its own
advice and skip the advice in this case would be not only less general,
but also pretty ugly to implement and unreliable.

> You could also have `callbefore', `callafter', etc.  Maybe it´s even
> possible to implement this, using add-advice.

Yup, just use :after or :before (in which case your advice won't receive
the `orig-fun' argument).


reply via email to

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