Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update protocols.adoc #654

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions content/reference/protocols.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ A protocol is a named set of named methods and their signatures, defined using h
----
(defprotocol AProtocol
"A doc string for AProtocol abstraction"
(bar [a b] "bar docs")
(baz [a] [a b] [a b c] "baz docs"))
(bar [this a] "bar docs")
(baz [this] [this a] [this a b] "baz docs"))
----

* No implementations are provided
Expand All @@ -59,14 +59,14 @@ Note that you do not need to use this interface with https://clojure.github.io/c
[source,clojure]
----
(defprotocol P
(foo [x])
(bar-me [x] [x y]))
(foo [this x])
(bar-me [this x] [this x y]))

(deftype Foo [a b c]
(deftype Foo [this a b c]
P
(foo [x] a)
(bar-me [x] b)
(bar-me [x y] (+ c y)))
(foo [this x] a)
(bar-me [this x] b)
(bar-me [this x y] (+ c y)))

(bar-me (Foo. 1 2 3) 42)
= > 45
Expand Down Expand Up @@ -107,7 +107,7 @@ extend takes a type/class (or interface, see below), a one or more protocol + fu
** but opens the door to incidental multiple inheritance of implementation
*** since a class can inherit from more than one interface, both of which implement the protocol
*** if one interface is derived from the other, the more derived is used, else which one is used is unspecified.
* The implementing fn can presume first argument is instanceof AType
* The implementing fn can presume first argument is instanceof AType. First argument is _**this**_ .
* You can implement a protocol on _**nil**_
* To define a default implementation of protocol (for other than nil) just use Object

Expand All @@ -120,19 +120,19 @@ Protocols are fully reified and support reflective capabilities via https://cloj
----
(extend-type MyType
Countable
(cnt [c] ...)
(cnt [this c] ...)
Foo
(bar [x y] ...)
(baz ([x] ...) ([x y zs] ...)))
(bar [this x] ...)
(baz ([this x] ...) ([this x y] ...)))

;expands into:

(extend MyType
Countable
{:cnt (fn [c] ...)}
{:cnt (fn [this c] ...)}
Foo
{:baz (fn ([x] ...) ([x y zs] ...))
:bar (fn [x y] ...)})
{:baz (fn ([this x] ...) ([this x y] ...))
:bar (fn [this x] ...)})
----

=== Guidelines for extension
Expand Down