Skip to content

Instantly share code, notes, and snippets.

@ANDRON94
Last active August 13, 2023 20:56
Show Gist options
  • Save ANDRON94/b699d24c09b577a09b79f3110580e833 to your computer and use it in GitHub Desktop.
Save ANDRON94/b699d24c09b577a09b79f3110580e833 to your computer and use it in GitHub Desktop.
Explanation of how 'eval-when' works in Common Lisp
;;; explanation for LOAD *.lisp
;;; 1. Load ignores :compile-toplevel, :load-toplevel
;;; Result: NIL is returned
;;; explanation for COMPILE
;;; 1. not-compile-time(NCT)
;;; 2. ignore mode, EVAL, remains in current mode
;;; Result: print to output 'foo-compile'
;;; explanation for LOAD *.fasl
;;; Result: form is not presented in compiled file
(eval-when (:compile-toplevel)
(print 'foo-compile))
;;; explanation for LOAD *.lisp
;;; 1. Load ignores :compile-toplevel, :load-toplevel
;;; Result: NIL is returned
;;; explanation for COMPILE
;;; 1. NCT
;;; 2. ignore mode, PROCESS, go to NCT
;;; Result: simply minimally compile form
;;; explanation for LOAD *.fasl
;;; 1. Load compiled form
;;; 2. Evaluate compiled form
;;; Result: print to output 'foo-load', return FOO-LOAD symbol
(eval-when (:load-toplevel)
(print 'foo-load))
;;; explanation for LOAD *.lisp
;;; 1. Load form for :execute option
;;; 2. Evaluate form
;;; Result: print to output 'foo-execute', return FOO-EXECUTE symbol
;;; explanation for COMPILE
;;; 1. NCT
;;; 2. NCT mode, discard, remains in current mode
;;; Result: form is discarded
;;; explanation for LOAD *.fasl
;;; Result: form is not presented in compiled file
(eval-when (:execute)
(print 'foo-execute))
(eval-when (:compile-toplevel :load-toplevel) ; Process subforms in CTT
(print 'foo-1) ; Evaluate it and then compile it, in load evaluates
(eval-when (:execute) ; Evaluate all subforms
(print 'foo-2) ; Evaluate it
(eval-when (:execute :load-toplevel) ; Evaluate it, ignores :load-toplevel option
(print 'foo-3))) ; Evaluate it
(eval-when (:execute :load-toplevel) ; Process subforms in CTT
(print 'foo-4) ; Evaluate it and then compile it, in load evaluates
(eval-when (:load-toplevel) ; Process subforms in NCT
(print 'foo-5) ; Compile it, in load evaluates
(eval-when (:execute :load-toplevel) ; Process subfroms in NCT
(print 'foo-6))))) ; Compile it, in load evaluates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment