应该用`cl-flet`或`cl-letf`代替`flet`? [英] Should `flet` be replaced with `cl-flet` or `cl-letf` ?
问题描述
我安装的一些elisp函数生成警告:
Some elisp functions that I have installed generate warnings:
`flet' is an obsolete macro (as of 24.3); use either `cl-flet' or `cl-letf'.
如果我只是将所有 flet
与 cl-flet
?
如果可以替换它们,哪一个更好?
Is it dangerous if I simply replace all flet
with cl-flet
?
If it is OK to replace them, which one is better?
如果更换它不是危险的,我会将拉请求发送到项目
If it is not dangerous to replace it, I'd send pull requests to the projects.
有没有理由不改变它?
推荐答案
flet
与 cl-flet
或 cl-letf不同
。
这更危险(也许更强大)。这就是为什么它被废弃了。
flet
isn't the same as either cl-flet
or cl-letf
.
It's more dangerous (and maybe more powerful). That's why it's being deprecated.
由于它是不同的(动态绑定一个函数名称),你必须考虑
在每种情况下,如果适合替换它 cl-flet
。
Since it's different (binds dynamically a function name), you have to think
in each case if it's appropriate to replace it with cl-flet
.
(defun adder (a b)
(+ a b))
(defun add-bunch (&rest lst)
(reduce #'adder lst))
(add-bunch 1 2 3 4)
;; 10
(flet ((adder (a b) (* a b)))
(add-bunch 1 2 3 4))
;; 24
(cl-flet ((adder (a b) (* a b)))
(add-bunch 1 2 3 4))
;; 10
请注意, cl-flet
绑定,所以加法器
的行为没有改变,
而 flet
做动态绑定 add-bunch
暂时产生阶乘。
Note that cl-flet
does lexical binding, so the behavior of adder
isn't changed,
while flet
does dynamic binding, which makes add-bunch
temporarily produce a factorial.
这篇关于应该用`cl-flet`或`cl-letf`代替`flet`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!