Compilation, eval-when and the Evaluation Model
(compile 'square) ; compile one function
(compile nil '(lambda (x) (* x x))) ; compile an anonymous function
(compile-file "app.lisp") ; → app.fasl
(load "app.fasl")
(disassemble 'square) ; show machine code (SBCL)
(function-lambda-expression #'square)25.1 Processing Times
Text → objects (reader macros, #.) Macroexpansion / compile time
Macros expand; compile-file processes top-level forms The fasl is loaded (load-time-value)
25.2 eval-when
defmacro, defconstant, defpackage, and in-package already have compile-time effects. Functions used by macros during expansion must be defined in an eval-when or in an earlier-loaded file.
load-time-value computes a value once at load time: (load-time-value (make-hash-table)).
Minimal compilation semantics differ between compile-file, compile, load of source, and the REPL. Remember that constants and literal objects may be coalesced by the file compiler.
;; Make a helper function available to macros at compile time
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun make-accessor-name (slot) (intern (format nil "GET-~a" slot))))
(defmacro define-accessor (slot)
`(defun ,(make-accessor-name slot) (obj) (getf obj ,(intern (symbol-name slot) :keyword))))