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

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

Re: How to shadow a function temporarily? (flet and cl-flet)


From: Alex Kost
Subject: Re: How to shadow a function temporarily? (flet and cl-flet)
Date: Sun, 26 Jan 2014 23:38:20 +0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Emanuel Berg (2014-01-26 22:32 +0400) wrote:

> Alex Kost <alezost@gmail.com> writes:
>
>> 1. The main question is: how can I override a
>> function with another compatible function (with the
>> same args) temporarily?
>
> ;;;; dynamic (Emacs wide)
>
> ;; first store the correct functions so we have them
> ;; (note: "subr" as in "subroutine", not "subtract")
> (setq addition-fun    (symbol-function '+)) ; #<subr +>
> (setq subtraction-fun (symbol-function '-)) ; #<subr ->
>
> ;; change + to do subtraction
> (fset '+ subtraction-fun)
> (+ 1 2 3) ; -4
>
> ;; revert
> (fset '+ addition-fun)
> (+ 1 2 3) ; 6
>
> ;;; lexical
>
> (cl-labels ((+ (&rest args) (apply '- args)))
>   (+ 1 2 3) ) ; -4
> (+ 1 2 3)     ;  still 6

Thank you, I know all that stuff.  That's not exactly what i need, but
your will to help always impress me.



reply via email to

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