Skip to content

Commit d655a28

Browse files
wojtekmachJosé Valim
authored andcommitted
Improve defprotocol/2 docs (#8548)
* `__protocol__` doesn't accept `:name` * Elaborate on atoms that `__protocol__` accepts * Change code example to doctest and fix it Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent 5529c5f commit d655a28

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4567,25 +4567,34 @@ defmodule Kernel do
45674567
45684568
Any protocol module contains three extra functions:
45694569
4570-
* `__protocol__/1` - returns the protocol name when `:name` is given, a
4571-
keyword list with the protocol functions and their arities when
4572-
`:functions` is given, and a list of the implementations when `:impls` is
4573-
given
4570+
* `__protocol__/1` - returns the protocol information. The function takes
4571+
one of the following atoms:
4572+
4573+
* `:consolidated?` - returns whether the protocol is consolidated
4574+
4575+
* `:functions` - returns keyword list of protocol functions and their arities
4576+
4577+
* `:impls` - if consolidated, returns `{:consolidated, modules}` with the list of modules
4578+
implementing the protocol, otherwise `:not_consolidated`
4579+
4580+
* `:module` - the protocol module atom name
45744581
45754582
* `impl_for/1` - receives a structure and returns the module that
45764583
implements the protocol for the structure, `nil` otherwise
45774584
45784585
* `impl_for!/1` - same as above but raises an error if an implementation is
45794586
not found
45804587
4581-
Enumerable.__protocol__(:functions)
4582-
#=> [count: 1, member?: 2, reduce: 3]
4588+
For example, for the `Enumerable` protocol we have:
4589+
4590+
iex> Enumerable.__protocol__(:functions)
4591+
[count: 1, member?: 2, reduce: 3, slice: 1]
45834592
4584-
Enumerable.impl_for([])
4585-
#=> Enumerable.List
4593+
iex> Enumerable.impl_for([])
4594+
Enumerable.List
45864595
4587-
Enumerable.impl_for(42)
4588-
#=> nil
4596+
iex> Enumerable.impl_for(42)
4597+
nil
45894598
45904599
## Consolidation
45914600

0 commit comments

Comments
 (0)