Skip to content

Commit 29addf9

Browse files
author
José Valim
committed
Update Access docs, closes #2388
1 parent 6e9f9f2 commit 29addf9

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/elixir/lib/access.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ defprotocol Access do
44
empowers the nested update functions in Kernel.
55
66
For instance, `foo[bar]` translates `Access.get(foo, bar)`.
7-
`Kernel.get_in/2`, `Kernel.put_in/3` and `Kernel.update_in/3`
8-
are also all powered by the Access protocol.
7+
`Kernel.get_in/2`, Kernel.put_in/3`, `Kernel.update_in/3` and
8+
`Kernel.get_and_update_in/3` are also all powered by the Access
9+
protocol.
910
1011
This protocol is implemented by default for keywords, maps
1112
and dictionary like types:
@@ -22,19 +23,24 @@ defprotocol Access do
2223
iex> star_ratings[1.5]
2324
"★☆"
2425
25-
The key access must be implemented using the `===` operator.
26+
The key comparison must be implemented using the `===` operator.
2627
"""
2728

2829
@doc """
2930
Accesses the given key in the container.
3031
"""
32+
@spec get(t, term) :: t
3133
def get(container, key)
3234

3335
@doc """
34-
Gets a value and updates the given key in one pass.
36+
Gets a value and updates the given `key` in one pass.
3537
36-
In case the key is not set, invokes the function passing nil.
38+
The function must receive the value for the given `key`
39+
(or `nil` if the key doesn't exist in `container`) and
40+
the function must return a tuple containing the `get`
41+
value and the new value to be stored in the `container`.
3742
"""
43+
@spec get_and_update(t, term, (term -> {get, term})) :: {get, t} when get: var
3844
def get_and_update(container, key, fun)
3945

4046
@doc false

0 commit comments

Comments
 (0)