请问magit源码中这里的require为什么要这样放置?
(cl-eval-when (load eval)
(require 'magit-status)
;;; ...
)
一种等价的方式是
(progn
(require 'magit-status)
;;; ...
)
后面这种方式应该如何理解?
require
自身已经被包裹在(cl-eval-when (compile load eval))
三种状态中,这里为何要专门用(cl-eval-when (load eval))
包裹?
下面是cl-eval-when
的源码
;;;###autoload
(defmacro cl-eval-when (when &rest body)
"Control when BODY is evaluated.
If `compile' is in WHEN, BODY is evaluated when compiled at top-level.
If `load' is in WHEN, BODY is evaluated when loaded after top-level compile.
If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level.
\(fn (WHEN...) BODY...)"
(declare (indent 1) (debug (sexp body)))
(if (and (macroexp-compiling-p)
(not cl--not-toplevel) (not (boundp 'for-effect))) ;Horrible kludge.
(let ((comp (or (memq 'compile when) (memq :compile-toplevel when)))
(cl--not-toplevel t))
(if (or (memq 'load when) (memq :load-toplevel when))
(if comp (cons 'progn (mapcar #'cl--compile-time-too body))
`(if nil nil ,@body))
(progn (if comp (eval (cons 'progn body) lexical-binding)) nil)))
(and (or (memq 'eval when) (memq :execute when))
(cons 'progn body))))
对cl-eval-when
的认识:
- 顶层形式
-
(cl-eval-when (compile) body ...)
在编译阶段,body
在顶层形式编译时求值; -
(cl-eval-when (load) body ...)
在加载阶段,body
在顶层形式编译后求值; -
(cl-eval-when (eval) body ...)
等同于progn
。