Protocols implementation affordances using defrecord/deftype vs. extend-protocol
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- clojure
- Lĩnh vực
- documentation
Hướng nghiên cứu
Xem lại danh sách gạch đầu dòng hiện tại về các tùy chọn triển khai protocol và so sánh các khả năng được thể hiện trong các ví dụ defrecord/deftype và extend-protocol của issue. Ghi lại các khác biệt trong một bảng tính năng toàn diện, bao gồm cả hành vi của điều kiện trước và điều kiện sau, để các đánh đổi trở nên rõ ràng hơn.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- HTML
- Star
- 259
- Fork
- 277
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của clojure/clojure-site
-
Document `do` behavior at top-levelCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
clojure/clojure-site#723 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
clojure/clojure-site#538 · 3 bình luận ·
-
Deps and CLI Guide: In MS Windows Control-C should be used instead of Control-DCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởhelp wanted
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
clojure/clojure-site#386 · 1 bình luận ·
-
Update Java Interop section of Learn Clojure > FunctionsCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 55/100
clojure/clojure-site#715 ·
-
Make it easier to find destructuring info?Có thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
clojure/clojure-site#713 ·
Tất cả issue của clojure/clojure-site
Issue tương tự
-
CalendarEventAttendance/get returns eventAttendanceStatus while the doc says attendanceStatusCó thể đã có người làm @chibenwa đã nhận hôm nay. Đang mởbug claude
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
linagora/tmail-backend#2697 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
PyPSA/PyPSA#1976 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
C-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày
-
status:untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
midnightntwrk/servicedesk#234 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Docs][openrouter]: README JWT example mints a token without exp, which the proxy always rejectsĐang mởgood first issue help wanted
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
Maintainer thường phản hồi trong vòng 1 ngày