Protocols - clarify access to parameters
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 45/100
- Tipo di issue
- Documentazione
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- clojure
- Ambito
- documentation
Direzione di ricerca
Inizia individuando le guide Protocols and Data Types nella documentazione di clojure-site e verifica come vengono attualmente spiegati i metodi dei protocolli, defrecord ed extend-protocol. Usa gli esempi Clojure dell'issue come riferimento per il chiarimento proposto; il lavoro è completato quando le guide spiegano chiaramente l'accesso alle proprietà e la denominazione dei parametri in questi casi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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?
- Lingua principale
- HTML
- Stelle
- 259
- Fork
- 275
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di clojure/clojure-site
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
clojure/clojure-site#723 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
clojure/clojure-site#538 · 3 commenti ·
-
help wanted
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 62/100
clojure/clojure-site#386 · 1 commento ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 55/100
clojure/clojure-site#715 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
clojure/clojure-site#713 ·
Tutte le issue di clojure/clojure-site
Issue simili
-
Link Checker Report Apertaautomated issue report
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
components-web-app/docs#99 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
TheOdinProject/curriculum#31423 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 75/100
-
hcocena Apertapolicies-accepted pre-review precheck-passed
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
Bioconductor/BiocContributions#214 · 5 commenti ·