'store-value'和'use-value'的语义在Common Lisp错误处理系统中重新启动

Pau*_*omé 3 lisp error-handling exception-handling common-lisp condition-system

我一直在阅读Peter Seibel Practical Common Lisp的优秀书籍,以解决我一直在做的与Common Lisp错误处理系统相关的研究.

虽然我已经阅读了书中的解释并试图在网上挖掘一些信息,但我无法理解STORE-VALUEUSE-VALUE重启的含义和用法.有人可以解释这些功能的目的是什么?

;;; Example of the STORE-VALUE and USE-VALUE restarts

(defun careful-symbol-value (symbol)
   (check-type symbol symbol)
   (restart-case (if (boundp symbol)
                     (return-from careful-symbol-value 
                                 (symbol-value symbol))
                     (error 'unbound-variable
                            :name symbol))
     (use-value (value)
       :report "Specify a value to use this time."
     value)
     (store-value (value)
       :report "Specify a value to store and use in the future."
       (setf (symbol-value symbol) value))))
Run Code Online (Sandbox Code Playgroud)

Rai*_*wig 7

这是Lispworks中的一个例子.

让我们定义一个foo带槽的类bar.

CL-USER 26 > (defclass foo () (bar))
#<STANDARD-CLASS FOO 4020001723>
Run Code Online (Sandbox Code Playgroud)

我们需要一个实例:

CL-USER 27 > (make-instance 'foo)
#<FOO 402000339B>
Run Code Online (Sandbox Code Playgroud)

现在我们尝试访问该对象的未绑定槽.请注意,*访问先前评估的结果.

CL-USER 28 > (slot-value * 'bar)
Run Code Online (Sandbox Code Playgroud)

我们收到一个错误和一堆重启:

Error: The slot BAR is unbound in the object #<FOO 402000339B>
    (an instance of class #<STANDARD-CLASS FOO 4020001723>).
  1 (continue) Try reading slot BAR again.
  2 Specify a value to use this time for slot BAR.
  3 Specify a value to set slot BAR to.
  4 (abort) Return to level 0.
  5 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.
Run Code Online (Sandbox Code Playgroud)

数字2是使用值重启,数字3是存储值重启.

我们来看一下重启列表:

CL-USER 29 : 1 > (compute-restarts)
(#<RESTART ABORT 4020009EB3>       #<RESTART ABORT 4020009F53>
 #<RESTART NIL 402000585B>         #<RESTART USE-VALUE 40200058DB>
 #<RESTART STORE-VALUE 402000595B> #<RESTART ABORT 40200059DB>
 #<RESTART ABORT 4020005A7B>       #<RESTART ABORT 41700D2503>)
Run Code Online (Sandbox Code Playgroud)

在LispWorks中,我们可以获取当前的条件对象:cc.

CL-USER 30 : 1 > :cc
#<UNBOUND-SLOT 40200056F3>
Run Code Online (Sandbox Code Playgroud)

找到重启:

CL-USER 31 : 1 > (find-restart 'store-value *)
#<RESTART STORE-VALUE 402000595B>
Run Code Online (Sandbox Code Playgroud)

我们打印出来:

CL-USER 32 : 1 > (princ *)
Specify a value to set slot BAR to.
#<RESTART STORE-VALUE 402000595B>
Run Code Online (Sandbox Code Playgroud)

也用于use-value重启:

CL-USER 33 : 1 > :cc
#<UNBOUND-SLOT 402000B293>

CL-USER 34 : 1 > (find-restart 'use-value *)
#<RESTART USE-VALUE 402000B47B>

CL-USER 35 : 1 > (princ *)
Specify a value to use this time for slot BAR.
#<RESTART USE-VALUE 402000B47B>
Run Code Online (Sandbox Code Playgroud)