- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我定义了一个使用验证器插槽增强标准插槽的元类,当我通过 :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") 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/
插槽到底是个啥?5分钟搞定 Vue 插槽 插槽的基本使用 组件使用slot标签,显示组件标签的内容 Title aaa Vu
Closed. This question is not reproducible or was caused by typos。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-to
我有一个对象,它用一个对象发出信号: MyObj *obj = this->generateObj(); emit newObjSignal(obj); delete obj; 我有一个或多个人与此有
距离ETS的发布也有一段时间,也有不少小伙伴通过ETS制作出很多精美的页面,但在我查阅ETS的组件和API中发现,现有版本的ETS并没有插槽的功能。经过一段时间的探索终于找到曲线救国方式实现插槽
我无法找到有关使用资源管理器模板以编程方式向 WebApp 添加插槽的信息/指南。我的基本配置运行良好,创建了 WebApp 本身、SQL 服务器、SQL DB 等,但我也渴望完成插槽,以便我可以将它
我们的项目位于 sitecore 中,并使用 Azure - 应用服务进行部署。我们已经创建了暂存槽,除了应用程序设置和连接字符串之外,还希望使一些配置文件槽特定(交换时坚持槽)。 有没有办法让整个配
我使用 Azure 插槽功能来部署我的机器人: 部署到“暂存”槽 Azure 启动此实例 启动后,我会交换“生产”和“登台”时段 我想在进行插槽之前测试暂存实例,最好使用 Azure 门户中的 Web
我使用 Azure 插槽功能来部署我的机器人: 部署到“暂存”槽 Azure 启动此实例 启动后,我会交换“生产”和“登台”时段 我想在进行插槽之前测试暂存实例,最好使用 Azure 门户中的 Web
我的应用程序只能通过右键单击托盘图标并按“退出”来退出: class DialogUIAg(QDialog): ... self.quitAction = QAction("&Quit
我不太确定如何将其总结为一个谷歌问题,但也许详细解释它会给我更好的帮助。 我正在尝试找到与在 PHP 中的 python 中设置槽的等价物 python : class Node: slots
我浪费了几个小时试图找出这个错误,但我仍然不知道为什么会发生...所以我求助于你! 我将一个对象移动到一个线程中,该线程执行一些工作,完成后会发出一个信号以供 QMainWindow 捕获。就这么简单
我有一个自旋控件 block ,它可以更改数组的各个元素我不想使用单独的接收器槽函数,而是只想指定哪个控件在信号中发送消息 您可以使用 QSignalMapper 来做到这一点——但是是否可以像下面那
我想知道如何将一个单独的变量传递到一个插槽中。我似乎无法让它工作。有什么办法解决这个问题吗? 这是我的代码: QTimer * timer = new QTimer(); connect(timer,
我有一个基类,它定义了一个 Qt 插槽 class Base { public: Base() { connect(otherobject, SIGNAL(mySign
这是基类中声明的样子: protected: void indexAll(); void cleanAll(); 在派生类中,以下不编译: indexAll(); // OK con
比如我有一个函数 void A::fun() { do_1(); emit signal_1(); do_2(); emit signal_2(); do_3(
我希望能够将视频从连接到我的计算机的摄像头直接流式传输到我通过 PCIE 连接到我的计算机的 FPGA。 我不介意使用 javascript 或 C# 等高级语言来执行此操作(因为这些是我知道的具有视
Qt世界中,事件和信号/槽的区别是什么? 一个会取代另一个吗?事件是信号/槽的抽象吗? 最佳答案 Qt documentation可能解释得最好: In Qt, events are objects,
1. 监听属性值的变化 - watch 语法要求:watch里面的函数名必须跟date里面属性名一致 {{num}} 添加 var
在 Class Buttons 中,我有一个 btnRightClicked 信号和一个 mousePressEvent 插槽: void Buttons::mousePressEvent(QMous
我是一名优秀的程序员,十分优秀!