Protocols implementation affordances using defrecord/deftype vs. extend-protocol
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
- Issue 类型
- 文档
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- clojure
调研方向
审查当前关于协议实现选项的项目符号列表,并比较 issue 的 defrecord/deftype 和 extend-protocol 示例中展示的能力。在一份全面的功能表中记录这些差异,包括前置条件和后置条件的行为,以便更清楚地了解其中的权衡。
由索引模型根据 Issue 内容生成。
描述
When implementing Protocols using defrecord there are some missing affordances.
As an example, :pre and :post conditions cannot be applied.
[ Yes, I know we will soon have spec but these affordances will not be deprecated AFAIK. ]
Affordances not available on defrecord or deftype
(defprotocol Squarer (square [x]))
(defrecord PosIntSquarer [x]
Squarer
(square [_] (* x x)))
; Usage
(defprotocol Squarer (square [x]))
;=> Squarer
(defrecord PosIntSquarer [x]
Squarer
(square [_] (* x x)))
;=> practice1.core.PosIntSquarer
(square (->PosIntSquarer 2))
;=> 4
;---^^^ All good
(square (->PosIntSquarer "A"))
;CompilerException java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number, compiling:(.../core.clj:162:1)
;---^^^ We would like to prevent this using a simple assertion
(defrecord PosIntSquarer [x]
Squarer
(square [_]
{:pre [(pos-int? x)]}
(* x x)))
;=> practice1.core.PosIntSquarer
(square (->PosIntSquarer "A"))
;CompilerException java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number, compiling:(.../core.clj:162:1)
;---^^^ The pre-condition is being ignored
(defrecord PosIntSquarer [x]
Squarer
(square [_]
{:pre [(pos-int? x)]
:post [(pos? %)]}
(* x x)))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: % in this context, compiling:(.../core.clj:158:13)
;---^^^ The post condition compilation fail shows that all affordances are not available
; Calling externally declared functions is one solution
(defn positive-square [x]
{:pre [(pos-int? x)]
:post [(pos? %)]}
(* x x))
;=> #'practice1.core/positive-square
(defrecord PosIntSquarer [x]
Squarer
(square [_]
(positive-square x)))
;=> practice1.core.PosIntSquarer
(square (->PosIntSquarer "A"))
;CompilerException java.lang.AssertionError: Assert failed: (pos-int? x), compiling:(.../core.clj:163:1)
Affordances are available via extend-protocol
(defrecord DirectSquarer [x])
=> practice1.core.DirectSquarer
(extend-protocol Squarer
DirectSquarer
(square [this]
{:pre [(pos-int? (:x this))]
:post [(pos? %)]}
(* (:x this) (:x this))))
;=> nil
(square (->PosIntSquarer 2))
;=> 4
(square (->PosIntSquarer "A"))
;CompilerException java.lang.AssertionError: Assert failed: (pos-int? x), compiling:(.../core.clj:178:1)
(square (->PosIntSquarer -2))
;CompilerException java.lang.AssertionError: Assert failed: (pos-int? x), compiling:(.../core.clj:176:1)
I don't want to claim this is a bug. But it's a sign that these options have pros and cons which are not currently well explained.
I would like to develop a more comprehensive table of features / affordances might be nicer than the current bullet list
- 主要语言
- HTML
- 星标
- 259
- 派生
- 275
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
clojure/clojure-site 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 62/100
clojure/clojure-site#723 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
clojure/clojure-site#538 · 3 条评论 ·
-
help wanted
难度 1/5 1 小时以内 新手友好度 62/100
clojure/clojure-site#386 · 1 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 55/100
clojure/clojure-site#715 ·
-
难度 5/5 一周以上 新手友好度 25/100
clojure/clojure-site#713 ·
查看 clojure/clojure-site 的全部 Issue
相似的 Issue
-
agent-ready documentation needs-triage
难度 1/5 1-3 小时 新手友好度 88/100
-
untriaged
难度 1/5 1 小时以内 新手友好度 88/100
dotnet/dotnet-api-docs#13095 ·
-
documentation
难度 1/5 1 小时以内 新手友好度 91/100
-
workflow-status page template still says reusable workflows are "triggered only by workflow_call:" 未关闭
难度 1/5 1 小时以内 新手友好度 92/100
-
refactor
难度 2/5 1-3 小时 新手友好度 78/100