gpt4 book ai didi

common-lisp - 自定义插槽选项不会对其参数应用任何缩减

转载 作者:行者123 更新时间:2023-12-04 19:03:48 25 4
gpt4 key购买 nike

假设我定义了一个使用验证器插槽增强标准插槽的元类,当我通过 :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") is not a funcallable。
 (CLAVIER:VALID-EMAIL
"The email is invalid") fell through ETYPECASE expression.
Wanted one of (FUNCTION SYMBOL).
[Condition of type SB-KERNEL:CASE-FAILURE]

最佳答案

就像上面的评论所说, 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定义类。

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

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

关于common-lisp - 自定义插槽选项不会对其参数应用任何缩减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30195608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com