小编Dan*_*rov的帖子

HANDLER-CASE替代品不是宏观

请考虑以下代码:

(define-condition some-condition (error) nil)

(defmethod print-object ((obj some-condition) stream)
  (format stream "HELLO THERE"))

(defmacro error-report-test-aux (fn-to-cause-error error-type-to-catch fn-to-handle-error expected-message)
  `(let ((result-message
           (handler-case (funcall ,fn-to-cause-error)
             (,error-type-to-catch (e) (funcall ,fn-to-handle-error e)))))
     (assert (string= result-message
                      ,expected-message))
     t))
Run Code Online (Sandbox Code Playgroud)

我可以像这样使用它:

(error-report-test-aux (lambda () (error 'some-condition)) 
                       some-condition 
                       #'princ-to-string 
                       "HELLO THERE")
Run Code Online (Sandbox Code Playgroud)

但我想创建error-report-test-aux一个函数而不是宏,这样我就可以在变量中传递一种条件.

简单地编写defun代替defmacro和删除反引号和逗号不起作用,因为handler-case它是宏并且它不会评估error-type-to-catch.

我的问题是:是否有类似的东西handler-case会评估它的参数(特别是条件类型参数)?

common-lisp

3
推荐指数
1
解决办法
121
查看次数

标签 统计

common-lisp ×1