emacs lisp 是否支持以词法方式重新定义函数?



最新版本的 Emacs 支持对 elisp 代码中的变量进行词法绑定。是否也可以用词法重新定义函数?换句话说,Emacs Lisp有类似lexical-flet的东西吗?

在 Emacs<24.3 中,你可以(require 'cl)然后使用 labels 。 在 Emacs-24.3 及更高版本中,您也可以执行(require 'cl-lib),然后使用 cl-fletcl-labels

所有这些都是"复杂的宏",生成看起来像(let ((fun (lambda (args) (body)))) ... (funcall fun my-args) ...)的代码,因为底层语言本身不支持本地函数定义。

labels,但我不知道这是否是你要找的:

(defun foo ()
  42)
(defun bar ()
  (foo))
(list
 (foo)
 (bar)
 (labels ((foo ()
               12))
   (list (foo)
         (bar)))
 (foo)
 (bar))

它返回(42 42 (12 42) 42 42) .

最新更新