Common Lisp 的合约库设计?
来自 Clojure 的背景,我被它的前置/后置条件作为契约设计基础的潜力所吸引:
;; sqr.clj
(defn sqr [n]
{:pre [(not= 0 n) (number? n)]
:post [(pos? %) (number? %)]}
(* n n))
(sqr 10)
;=> 100
(sqr 0)
; Assertion error
Common Lisp 中是否有类似的前置/后置功能和/或更全面的契约设计图书馆在野外可用吗?
谢谢
Coming from a background in Clojure, I am taken with the potential that its pre-/post-conditions provide as a basis for design by contract:
;; sqr.clj
(defn sqr [n]
{:pre [(not= 0 n) (number? n)]
:post [(pos? %) (number? %)]}
(* n n))
(sqr 10)
;=> 100
(sqr 0)
; Assertion error
Is there a similar pre/post capability in Common Lisp and/or a more comprehensive Design by Contract library available in the wild?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编写一个可以像这样使用的宏相对简单:
对于 CLOS 通用函数,请参见此处: http://www.muc.de/~hoelzl/tools/dbc/dbc-intro.html
顺便说一句,从这段代码可以看出,零代码交换是可以在 CL 和 Clojure 之间实现,而无需完全重写任何内容。
it is relatively trivial to write a macro that can be used like this:
For CLOS generic functions, see here: http://www.muc.de/~hoelzl/tools/dbc/dbc-intro.html
Btw., from this code it can be seen that there is zero code exchange is possible between CL and Clojure, without rewriting anything completely.
您可以断言:
不确切知道帖子部分要做什么。 :)
You can assert:
Don't know exactly what the post part is ment to do. :)