English 中文(简体)
Eval - 何时使用?
原标题:Eval-when uses?

在阅读了大量有关Lisp eval- when 运算符的文件后, 我仍然无法理解它的用途, 我知道这个运算符能控制我表达式的评价时间, 但我无法找到任何可能适用的例子?

Best Regards, utxeee.

最佳回答

<% 1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ <\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

例如, Lisp 文件的编译。 Lisp 编译器处理顶层表格。 这些可以是任意的 Lisp 格式、 DeFUN 格式、 DeFMACROS 格式、 DeFMACROS 格式、 DeFCLASS 格式、 函数调用...

整个文件编译器如何工作的故事太复杂, 无法在此解释, 但有几件事:

  • 文件编纂器为 (DEFUN foo ()) form ()) < /code> 生成代码。但它不执行 defun 窗体。因此,在汇编过程中,已知有一个函数 FOO ,但在汇编过程中找不到 {FOO___的代码。编纂器为汇编的文件生成代码,但不会将其保存在内存中。您无法在汇编时调用此函数 。

  • 对于宏,它的工作略为不同: (DEFMACRO BAZ...) 。文件汇编器不仅将汇编宏并注意它存在,而且还会在汇编时提供宏。它被装入汇编器环境

因此,想象文件中的表格序列 :

(defmacro baz ...)

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

这是因为文件编译者知道宏 BA/code>, 当它编辑 FOO 的代码时, 它可以扩展宏表单 。

现在让我们看看下面的例子:

(defun bar (form) ...)

(defmacro baz (form) (bar form))

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

上面没有工作。 现在, 宏 < code>BA_ {/ code> 使用函数 < code>BAR 来调用它。 当编译者试图编译函数 < code> FOO 时, 它无法扩展 < code>BA_ {/ code> 宏, 因为 < code> BAR 无法被调用, 因为 < code> BAR 的代码没有被装入编译时间环境 。

对此有两种解决办法:

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

EVAL-WHEN 示例:

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

 (defmacro baz (form) (bar form))

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

现在 EVAL-WHEN 指示文件编纂者在汇编过程中实际运行 DEFUN 格式。 效果是: 文件编纂者现在在 < em> complile time 上知道 < code> 的定义 < code> < bAR 。 因此,在以后, 当文件编纂者在宏扩展 < code> BaBA_ /code的使用时需要调用 < code> < bAR 时, 就可以使用 。

人们只能使用 :compile-toplevel , 当文件汇编后不需要此函数时。 如果稍后使用, 那么我们需要确保它被装入 。

EVAL-WHEN 允许指定是否应该运行特定的代码件

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

EVAL-WHEN 在用户代码中没有经常使用。 如果您使用它, 那么您应该问问自己是否真的需要它 。

问题回答

暂无回答




相关问题
Lisp code called from Java

Long story: I am doing a project for my functional programing class, and I thought of writing an AI controller in Lisp, for the Mario AI competition. I was looking over frameworks/libraries/ways of ...

Emacs, Zen-Coding mode, and Putty

I use emacs via Putty and since Putty doesn t send certain key combinations to the remote console I generally need to re-bind them to other key combinations. After installing the amazing Zen-Coding ...

In Which Cases Is Better To Use Clojure? [closed]

I develop in Lisp and in Scheme, but I was reading about Clojure and then I want to know, in which cases is better to use it than using Lisp or Scheme? Thanks

lambda-gtk negative pointer

I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative ...

Is there a common lisp package naming convention?

I have created some of my own user packages and have run into a name clash. In Java, the naming convention is to use your domain name in the package name: e.g. import com.example.somepackage;. Are ...

SOAP request from within an AutoLISP/AutoCAD macro

We have built a webservice for a client that uses AutoCAD. They have a macro that runs in AutoCAD that builds a SOAP request. But they have not figured out how to actually send() the soap request to ...

热门标签