@@ -4,8 +4,9 @@ defprotocol Access do
4
4
empowers the nested update functions in Kernel.
5
5
6
6
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.
9
10
10
11
This protocol is implemented by default for keywords, maps
11
12
and dictionary like types:
@@ -22,19 +23,24 @@ defprotocol Access do
22
23
iex> star_ratings[1.5]
23
24
"★☆"
24
25
25
- The key access must be implemented using the `===` operator.
26
+ The key comparison must be implemented using the `===` operator.
26
27
"""
27
28
28
29
@ doc """
29
30
Accesses the given key in the container.
30
31
"""
32
+ @ spec get ( t , term ) :: t
31
33
def get ( container , key )
32
34
33
35
@ 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.
35
37
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`.
37
42
"""
43
+ @ spec get_and_update ( t , term , ( term -> { get , term } ) ) :: { get , t } when get: var
38
44
def get_and_update ( container , key , fun )
39
45
40
46
@ doc false
0 commit comments