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: Stefan Monnier
Subject: Re: How to shadow a function temporarily? (flet and cl-flet)
Date: Sun, 26 Jan 2014 09:50:15 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

> 1. The main question is: how can I override a function with another
> compatible function (with the same args) temporarily?

Be careful with "temporarily": it can be interpreted as "lexically" or
"dynamically".  From your example, it seems you want "dynamically".
Note, tho, that

   (defun 8+ (arg)
     (+ 8 arg))
   
   (flet ((+ (&rest args)
             (apply '- args)))
     (8+ 3))                               ; => 5

will give you 11 when byte-compiled.  IOW, you had better look for
another solution to your problem.  One option could be something along
the following lines:

   (defvar my-plus-is-subtraction nil)

   (defun 8+ (arg)
     (funcall (if my-plus-is-subtraction #'- #'+) 8 arg))
   
   (let ((my-plus-is-subtraction t))
     (8+ 3))                               ; => 5


-- Stefan




reply via email to

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