【问题标题】:Custom slot options don't apply any reduction to its argument自定义插槽选项不会对其参数应用任何缩减
【发布时间】:2015-05-12 15:41:34
【问题描述】:

假设我定义了一个使用验证器插槽增强标准插槽的元类,当我将:validator (clavier:valid-email "The email is invalid") 作为选项传递时,它不会存储表达式的结果(这是一个可调用函数),而是存储表达式本身。扩展标准插槽时我错过了一步吗?如何确保在存储之前评估表达式?我正在使用 SBCL 1.2.11 顺便说一句。这是有问题的代码

(unless (find-package 'clavier)
  (ql:quickload :clavier))
(unless (find-package 'c2mop)
  (ql:quickload :c2mop))
(defpackage #:clos2web/validation
  (:use #:cl)
  (:import-from #:c2mop
                #:standard-class
                #:standard-direct-slot-definition
                #:standard-effective-slot-definition
                #:validate-superclass
                #:direct-slot-definition-class
                #:effective-slot-definition-class
                #:compute-effective-slot-definition
                #:slot-value-using-class))

(in-package #:clos2web/validation)

(defun true (value)
  "Always return true."
  (declare (ignore value))
  t)

(defclass validation-class (standard-class)
  ()
  (:documentation "Meta-class for objects whose slots know how to validate
  their values."))

(defmethod validate-superclass
    ((class validation-class) (super standard-class))
  t)

(defmethod validate-superclass
    ((class standard-class) (super validation-class))
  t)

(defclass validation-slot (c2mop:standard-slot-definition)
  ((validator :initarg :validator :accessor validator :initform #'true
              :documentation "The function to determine if the value is
  valid. It takes as a parameter the value.")))

(defclass validation-direct-slot (validation-slot
                                  standard-direct-slot-definition)
  ())

(defclass validation-effective-slot (validation-slot
                                     standard-effective-slot-definition)
  ())

(defmethod direct-slot-definition-class ((class validation-class) &rest initargs)
  (declare (ignore initargs))
  (find-class 'validation-direct-slot))

(defmethod effective-slot-definition-class ((class validation-class) &rest initargs)
  (declare (ignore initargs))
  (find-class 'validation-effective-slot))

(defmethod compute-effective-slot-definition
    ((class validation-class) slot-name direct-slot-definitions)
  (let ((effective-slot-definition (call-next-method)))
    (setf (validator effective-slot-definition)
          (some #'validator direct-slot-definitions))
    effective-slot-definition))

(defmethod (setf slot-value-using-class) :before
    (new (class validation-class) object (slot validation-effective-slot))
  (when (slot-boundp slot 'validator)
    (multiple-value-bind (validp msg)
        (funcall (validator slot) new)
      (unless validp
        (error msg)))))

;; Example usage

(defclass user ()
  ((name :initarg :name)
   (email :initarg :email :validator (clavier:valid-email "The email is invalid") :accessor email))
  (:metaclass validation-class))

(let ((pepe (make-instance 'user :name "Pepe" :email "pepe@tumadre.com")))
  (setf (email pepe) "FU!")) ;; should throw

创建实例时代码失败,因为 (CLAVIER:VALID-EMAIL "The email is invalid") 不是可调用的。

 (CLAVIER:VALID-EMAIL
  "The email is invalid") fell through ETYPECASE expression.
 Wanted one of (FUNCTION SYMBOL).
    [Condition of type SB-KERNEL:CASE-FAILURE]

【问题讨论】:

  • Defclass 不评估事物,因此表单存储为插槽选项。我正在考虑最好的解决方案。
  • 在 sbcl 中,canonicalize-defclass-slots 负责处理插槽并可以访问定义的环境,您是否有任何关于如何使其适合使用来减少非标准选项的指示环境?

标签: common-lisp clos mop


【解决方案1】:

就像上面的评论所说,defclass 不评估参数(它是一个宏)。虽然通常的建议是避免 eval,但我认为在这种情况下 eval 可能正是您想要的。虽然通常您会将表单直接拼接到某个宏体中,但使用 defclass 我认为答案是在插槽初始化中评估表单并存储评估(如果尚未评估)。

这可能发生在:

(defmethod initialize-instance :after ((obj validation-slot)
                                       &key &allow-other-keys)
  #| ... |#)

您也可以选择将:validation-message:validation-fn 存储为两个单独的参数,然后调用:

(multiple-value-bind (validp msg)
    (funcall (funcall (validator-fn slot)
                      (validator-message slot))
             new)
  (unless validp
    (error msg)))

另一种选择是存储表单的评估并将其传递给宏:

(defvar *email-validator* (CLAVIER:VALID-EMAIL "The email is invalid"))
(defun email-validator (val)
  (funcall *email-validator* val))

然后将email-validator 传递给defclass。

此外,我可能会建议您的验证函数发出slot-validation-error 类型条件而不是error 类型条件。然后,您的条件可能包含对失败的验证器、值、插槽和实例的引用。与原始错误相比,这可以为您提供更好的控制。您还可以添加一些重启(abort 跳过设置槽,使用值提供不同的值)。

根据您的设置,您的验证函数也可能更有意义,直接发出信号而不是返回多个值,然后将这些值强制转换为信号。

【讨论】:

  • 感谢插槽验证错误的指针。好吧,我想它现在是 eval 。我可能应该看看 initform 在定义时如何跟踪环境以查看另一种选择。
猜你喜欢
  • 1970-01-01
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多