Should `flet` be replaced with `cl-flet` or `cl-letf` ?

后端 未结 4 1131
天命终不由人
天命终不由人 2021-01-07 21:20

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\'.
4条回答
  •  感动是毒
    2021-01-07 22:06

    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.

    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.

    Small example when flet can't be replaced 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
    

    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.

提交回复
热议问题