15

After reading a lot of documentation regarding Lisp eval-when operator I still can't understand its uses, I know with this operator I can control the evaluation time of my expressions but I can't figure out any example where this may be applicable ?

Best Regards, utxeee.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
utxeee
  • 953
  • 1
  • 12
  • 24

1 Answers1

28

Compilation of a Lisp file

Take for example the compilation of a Lisp file. The Lisp compiler processes the top-level forms. These can be arbitrary Lisp forms, DEFUNs, DEFMACROS, DEFCLASS, function calls,...

The whole story how the file compiler works is too complex to explain here, but a few things:

  • the file compiler generates code for a (DEFUN foo () ) form. But it does not execute the defun form. Thus during compilation it is known that there is a function FOO, but the code of ˋFOOˋ is not available during the compilation. The compiler generates the code for the compiled file, but does not keep it in memory. You can't call such a function at compile time.

  • for macros this works slightly different: (DEFMACRO BAZ ...). The file compiler will not only compile the macro and note that it is there, but it will also make the macro available at compilation time. It is loaded into the compiler environment.

Thus imagine the sequence of forms in a file:

(defmacro baz ...)

(defun foo () (baz ...))

This works because the file compiler knows the macro BAZ and when it compiles the code for FOO, then it can expand the macro form.

Now let's look at the following example:

(defun bar (form) ...)

(defmacro baz (form) (bar form))

(defun foo () (baz ...))

Above will not work. Now the macro BAZ uses the function BAR by calling it. When the compiler tries to compile the function FOO, it can't expand the BAZ macro, because BAR can't be called, because the code of BAR is not loaded into the compile-time environment.

There are two solutions to this:

  1. compile and load BAR earlier using a separate file.
  2. Use EVAL-WHEN

Example for EVAL-WHEN:

 (eval-when (:compile-toplevel :execute :load-toplevel)
   (defun bar (form) ...)
 )

 (defmacro baz (form) (bar form))

 (defun foo () (baz ...))

Now the EVAL-WHEN instructs the file compiler to actually run the DEFUN form during compilation. The effect of this is: the file compiler now knows the definition of BAR at compile time. Thus it is available later, when the file compiler need to call BAR during macro expansion of the usage of BAZ.

One could use only :compile-toplevel, when the function would not be needed after the compilation of the file. If it is used later, then we need to make sure that it gets loaded.

So EVAL-WHEN allows to specify if a particular piece of code should be run

  • during compilation of a file
  • during loading of a file
  • during execution

EVAL-WHEN is not used that often in user code. If you use it, then you should ask yourself if you really need it.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • 1
    Hi Rainer, but why we need to use the flags :execute and :load-top-level in this use of eval-when ? – utxeee May 20 '12 at 19:16
  • 1
    Rainer thanks again for your reply editing the previous answer :D But my doubt remains, why I need to use the flags :load-top-level and :execute. From your text I can see that we should use the flag :load-top-level if the function is used later. So why we don't need to use the eval-when with :load-top-level in all the other functions that will be used later ? – utxeee May 20 '12 at 23:35
  • 1
    @utxeee: load-top-level is a default during compilation, when there is no EVAL-WHEN. – Rainer Joswig May 21 '12 at 10:37
  • Rainer, two more questions : (1) - if we have loaded the file from source code with your example there wasn't the need to use eval-when operator, because the bar definition was already known, right ? (2) - Why we need to use the keyword :execute - as far as I know this keyword is related with run-time but at run-time the definition of a function is already known too, right ? – utxeee May 21 '12 at 13:20
  • 2
    could you please fix the typo `load-top-level` ==> `:load-toplevel`? I had to dig into the source to find that – ealfonso Sep 13 '15 at 18:56
  • Regarding your first example: **This works because the file compiler knows the macro BAZ and when it compiles the code for FOO, then it can expand the macro form.**, if the compiler doesn't execute the `defun` forms anyway, when is the expansion of `baz` going to happen, since it is in the non-top-level body form of `foo`. – Student May 12 '20 at 23:43
  • @Student The file compiler will expand the macros in code, since it needs that in order to compile the code. It does not execute the DEFUN, but it compiles it and generates code (for example machine code) for the whole function definition form - including its subforms. – Rainer Joswig May 13 '20 at 07:09
  • Are there also any aspects of eval-when controlled by the implementation? – Student May 19 '20 at 21:33