Protocols - clarify access to parameters
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 45/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
Bắt đầu bằng cách tìm các hướng dẫn Protocols and Data Types trong tài liệu của clojure-site và xem lại cách các phương thức protocol, defrecord và extend-protocol hiện được giải thích. Sử dụng các ví dụ Clojure trong issue làm tài liệu tham khảo cho phần làm rõ được đề xuất; được xem là hoàn tất khi các hướng dẫn giải thích rõ việc truy cập thuộc tính và cách đặt tên tham số trong các trường hợp này.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
There are various ways to define and access properties defined with protocols and data types which are non-obvious and not explained or clarified in the Protocols or Data Types guides
I propose updating the Protocol guide with this examples:
Names and values when using defrecord directly
(defprotocol DeviceRegistration
"Protocol to register various IOT devices"
(register [device])
(ping [device]))
(defrecord RaspberryPi [device-id location registry-id]
DeviceRegistration
(register [_]
(->RaspberryPi device-id location 789))
(ping [_]
(= 789 registry-id)))
register and ping ignore the method parameters. Instead they uses the properties directly from the record.
Sample Usage
(def rasp-pi (map->RaspberryPi {:device-id 123 :location {:x 123.4 :y 432.1}}))
;=> #'practice1.core/rasp-pi
(def registered-pi (register rasp-pi))
;=> #'practice1.core/registered-pi
(println (type registered-pi))
;practice1.core.RaspberryPi
(def pinged? (ping registered-pi))
;=> #'practice1.core/pinged?
(println pinged?)
; true
Note: Diligent readers will have observed that we can only ever register or ping one RaspberryPi using this code. It's good enough for the example but maybe not production quality.
Names and values when using extend-protocol
(defrecord Arduino [device-id location registry-id])
(extend-protocol DeviceRegistration
Arduino
(register [device]
(assoc device :registry-id 890))
(ping [device]
(= 890 (:registry-id device))))
register and ping do not have access to the record directly. They access record properties from the method parameter, which is the Arduino record.
register creates a new Arduino record via assoc. In this case it is simpler than creating a new Arduino record using either ->Arduino or map->Arduino functions supplied by defrecord.
Sample Usage
(def arduino (map->Arduino {:device-id 345 :location {:x 432.1 :y 987.6}}))
;=> #'practice1.core/arduino
(def registered-arduino (register arduino))
;=> #'practice1.core/registered-arduino
(println (type registered-arduino))
;practice1.core.Arduino
(def pinged? (ping registered-arduino))
;=> #'practice1.core/pinged?
(println pinged?)
;true
Code in the wild uses this
Most code I see reads more like, ahem, this:
(extend-protocol DeviceRegistration
Arduino
(register [this]
(assoc this :registry-id 890))
(ping [this]
(= 890 (:registry-id this))))
In some languages there is a keyword this or self and it has a special meaning. It kind of does in these cases too - even though the symbol itself is not special in Clojure. Rather than use conventionally privileged words, I prefer the more descriptive option.
Obligatory Clojure documentation rant
Following on from the this mini-rant, I am not a fan of x y z and foo and bar as example parameters or method calls.
Documentation comes to life when it is illustrated with simple and easily understood examples rather than abstract characters and meaningless phrases.
I know 'real things' come and go but these guides are not written to see the heat death of the universe. Are they?
- 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