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: Emanuel Berg
Subject: Re: How to shadow a function temporarily? (flet and cl-flet)
Date: Sun, 26 Jan 2014 19:32:34 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

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

-- 
underground experts united:
http://user.it.uu.se/~embe8573


reply via email to

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