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

后端 未结 4 1123
天命终不由人
天命终不由人 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:03

    You could modify your function to use lawlist-flet or create an alias -- all I did was remove the warning and rename the flet macro to lawlist-flet:

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;; FLET ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    (defmacro lawlist-flet (bindings &rest body)
          "Make temporary overriding function definitions.
        This is an analogue of a dynamically scoped `let' that operates on the function
        cell of FUNCs rather than their value cell.
        If you want the Common-Lisp style of `flet', you should use `cl-flet'.
        The FORMs are evaluated with the specified function definitions in place,
        then the definitions are undone (the FUNCs go back to their previous
        definitions, or lack thereof).
        \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
          (declare (indent 1) (debug cl-flet)
        ;;           (obsolete "use either `cl-flet' or `cl-letf'."  "24.3")
                        )
          `(letf ,(mapcar
                   (lambda (x)
                     (if (or (and (fboundp (car x))
                                  (eq (car-safe (symbol-function (car x))) 'macro))
                             (cdr (assq (car x) macroexpand-all-environment)))
                         (error "Use `labels', not `flet', to rebind macro names"))
                     (let ((func `(cl-function
                                   (lambda ,(cadr x)
                                     (cl-block ,(car x) ,@(cddr x))))))
                       (when (cl--compiling-file)
                         ;; Bug#411.  It would be nice to fix this.
                         (and (get (car x) 'byte-compile)
                              (error "Byte-compiling a redefinition of `%s' \
        will not work - use `labels' instead" (symbol-name (car x))))
                         ;; FIXME This affects the rest of the file, when it
                         ;; should be restricted to the flet body.
                         (and (boundp 'byte-compile-function-environment)
                              (push (cons (car x) (eval func))
                                    byte-compile-function-environment)))
                       (list `(symbol-function ',(car x)) func)))
                   bindings)
             ,@body))
    

提交回复
热议问题