Skip to content

Commit dad1b86

Browse files
Streamline get_and_update specs for Access for Kernel (#10822)
In the same fashion of PRs #10228 and #10223 Replaces: - `get_values` with `current_value` - `update_value` with `new_value` - `data` with `new_data` when suitable Adds nil as an argument term in the function spec.
1 parent 251413a commit dad1b86

File tree

4 files changed

+47
-44
lines changed

4 files changed

+47
-44
lines changed

lib/elixir/lib/access.ex

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ defmodule Access do
9999
@type key :: any
100100
@type value :: any
101101

102-
@type get_fun(data, get_value) ::
102+
@type get_fun(data, current_value) ::
103103
(:get, data, (term -> term) ->
104-
{get_value, new_data :: container})
104+
{current_value, new_data :: container})
105105

106-
@type get_and_update_fun(data, get_value) ::
106+
@type get_and_update_fun(data, current_value) ::
107107
(:get_and_update, data, (term -> term) ->
108-
{get_value, new_data :: container} | :pop)
108+
{current_value, new_data :: container} | :pop)
109109

110-
@type access_fun(data, get_value) ::
111-
get_fun(data, get_value) | get_and_update_fun(data, get_value)
110+
@type access_fun(data, current_value) ::
111+
get_fun(data, current_value) | get_and_update_fun(data, current_value)
112112

113113
@doc """
114114
Invoked in order to access the value stored under `key` in the given term `term`.
@@ -134,16 +134,16 @@ defmodule Access do
134134
135135
The implementation of this callback should invoke `fun` with the value under
136136
`key` in the passed structure `data`, or with `nil` if `key` is not present in it.
137-
This function must return either `{get_value, update_value}` or `:pop`.
137+
This function must return either `{current_value, new_value}` or `:pop`.
138138
139-
If the passed function returns `{get_value, update_value}`,
140-
the return value of this callback should be `{get_value, new_data}`, where:
139+
If the passed function returns `{current_value, new_value}`,
140+
the return value of this callback should be `{current_value, new_data}`, where:
141141
142-
* `get_value` is the retrieved value (which can be operated on before being returned)
142+
* `current_value` is the retrieved value (which can be operated on before being returned)
143143
144-
* `update_value` is the new value to be stored under `key`
144+
* `new_value` is the new value to be stored under `key`
145145
146-
* `new_data` is `data` after updating the value of `key` with `update_value`.
146+
* `new_data` is `data` after updating the value of `key` with `new_value`.
147147
148148
If the passed function returns `:pop`, the return value of this callback
149149
must be `{value, new_data}` where `value` is the value under `key`
@@ -152,8 +152,9 @@ defmodule Access do
152152
See the implementations of `Map.get_and_update/3` or `Keyword.get_and_update/3`
153153
for more examples.
154154
"""
155-
@callback get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
156-
when get_value: var, data: container | any_container
155+
@callback get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
156+
{current_value, new_data :: data}
157+
when current_value: value, data: container | any_container
157158

158159
@doc """
159160
Invoked to "pop" the value under `key` out of the given data structure.
@@ -320,9 +321,9 @@ defmodule Access do
320321
a struct that implements the `Access` behaviour).
321322
322323
The `fun` argument receives the value of `key` (or `nil` if `key` is not
323-
present in `container`) and must return a two-element tuple `{get_value, update_value}`:
324-
the "get" value `get_value` (the retrieved value, which can be operated on before
325-
being returned) and the new value to be stored under `key` (`update_value`).
324+
present in `container`) and must return a two-element tuple `{current_value, new_value}`:
325+
the "get" value `current_value` (the retrieved value, which can be operated on before
326+
being returned) and the new value to be stored under `key` (`new_value`).
326327
`fun` may also return `:pop`, which means the current value
327328
should be removed from the container and returned.
328329
@@ -337,8 +338,9 @@ defmodule Access do
337338
{1, [a: 2]}
338339
339340
"""
340-
@spec get_and_update(data, key, (value -> {get_value, value} | :pop)) :: {get_value, data}
341-
when get_value: var, data: container
341+
@spec get_and_update(data, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
342+
{current_value, new_data :: data}
343+
when current_value: var, data: container
342344
def get_and_update(container, key, fun)
343345

344346
def get_and_update(%module{} = container, key, fun) do
@@ -451,7 +453,7 @@ defmodule Access do
451453
** (BadMapError) expected a map, got: []
452454
453455
"""
454-
@spec key(key, term) :: access_fun(data :: struct | map, get_value :: term)
456+
@spec key(key, term) :: access_fun(data :: struct | map, current_value :: term)
455457
def key(key, default \\ nil) do
456458
fn
457459
:get, data, next ->
@@ -495,7 +497,7 @@ defmodule Access do
495497
** (RuntimeError) Access.key!/1 expected a map/struct, got: []
496498
497499
"""
498-
@spec key!(key) :: access_fun(data :: struct | map, get_value :: term)
500+
@spec key!(key) :: access_fun(data :: struct | map, current_value :: term)
499501
def key!(key) do
500502
fn
501503
:get, %{} = data, next ->
@@ -543,7 +545,7 @@ defmodule Access do
543545
** (RuntimeError) Access.elem/1 expected a tuple, got: %{}
544546
545547
"""
546-
@spec elem(non_neg_integer) :: access_fun(data :: tuple, get_value :: term)
548+
@spec elem(non_neg_integer) :: access_fun(data :: tuple, current_value :: term)
547549
def elem(index) when is_integer(index) and index >= 0 do
548550
pos = index + 1
549551

@@ -597,7 +599,7 @@ defmodule Access do
597599
** (RuntimeError) Access.all/0 expected a list, got: %{}
598600
599601
"""
600-
@spec all() :: access_fun(data :: list, get_value :: list)
602+
@spec all() :: access_fun(data :: list, current_value :: list)
601603
def all() do
602604
&all/3
603605
end
@@ -672,7 +674,7 @@ defmodule Access do
672674
** (RuntimeError) Access.at/1 expected a list, got: %{}
673675
674676
"""
675-
@spec at(integer) :: access_fun(data :: list, get_value :: term)
677+
@spec at(integer) :: access_fun(data :: list, current_value :: term)
676678
def at(index) when is_integer(index) do
677679
fn op, data, next -> at(op, data, index, next) end
678680
end
@@ -727,7 +729,7 @@ defmodule Access do
727729
728730
"""
729731
@doc since: "1.11.0"
730-
@spec at!(integer) :: access_fun(data :: list, get_value :: term)
732+
@spec at!(integer) :: access_fun(data :: list, current_value :: term)
731733
def at!(index) when is_integer(index) do
732734
fn op, data, next -> at!(op, data, index, next) end
733735
end
@@ -794,7 +796,7 @@ defmodule Access do
794796
795797
"""
796798
@doc since: "1.6.0"
797-
@spec filter((term -> boolean)) :: access_fun(data :: list, get_value :: list)
799+
@spec filter((term -> boolean)) :: access_fun(data :: list, current_value :: list)
798800
def filter(func) when is_function(func) do
799801
fn op, data, next -> filter(op, data, func, next) end
800802
end

lib/elixir/lib/kernel.ex

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,8 +2661,8 @@ defmodule Kernel do
26612661
The `fun` argument receives the value of `key` (or `nil` if `key`
26622662
is not present) and must return one of the following values:
26632663
2664-
* a two-element tuple `{get_value, new_value}`. In this case,
2665-
`get_value` is the retrieved value which can possibly be operated on before
2664+
* a two-element tuple `{current_value, new_value}`. In this case,
2665+
`current_value` is the retrieved value which can possibly be operated on before
26662666
being returned. `new_value` is the new value to be stored under `key`.
26672667
26682668
* `:pop`, which implies that the current value under `key`
@@ -2716,13 +2716,14 @@ defmodule Kernel do
27162716
`Access.key/2`, and others as examples.
27172717
"""
27182718
@spec get_and_update_in(
2719-
structure :: Access.t(),
2719+
structure,
27202720
keys,
2721-
(term -> {get_value, update_value} | :pop)
2722-
) :: {get_value, structure :: Access.t()}
2723-
when keys: nonempty_list(any),
2724-
get_value: var,
2725-
update_value: term
2721+
(term | nil -> {current_value, new_value} | :pop)
2722+
) :: {current_value, new_structure :: structure}
2723+
when structure: Access.t(),
2724+
keys: nonempty_list(any),
2725+
current_value: Access.value(),
2726+
new_value: Access.value()
27262727
def get_and_update_in(data, keys, fun)
27272728

27282729
def get_and_update_in(data, [head], fun) when is_function(head, 3),

lib/elixir/lib/keyword.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ defmodule Keyword do
287287
{nil, [a: 1]}
288288
289289
"""
290-
@spec get_and_update(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
291-
{current_value, value}
290+
@spec get_and_update(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
291+
{current_value, new_keywords :: t}
292292
when current_value: value
293293
def get_and_update(keywords, key, fun)
294294
when is_list(keywords) and is_atom(key),
@@ -351,8 +351,8 @@ defmodule Keyword do
351351
{1, []}
352352
353353
"""
354-
@spec get_and_update!(t, key, (value -> {current_value, new_value :: value} | :pop)) ::
355-
{current_value, t}
354+
@spec get_and_update!(t, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
355+
{current_value, new_keywords :: t}
356356
when current_value: value
357357
def get_and_update!(keywords, key, fun) do
358358
get_and_update!(keywords, key, fun, [])
@@ -929,7 +929,7 @@ defmodule Keyword do
929929
[a: 1, b: 11]
930930
931931
"""
932-
@spec update(t, key, default :: value, (existing_value :: value -> updated_value :: value)) :: t
932+
@spec update(t, key, default :: value, (existing_value :: value -> new_value :: value)) :: t
933933
def update(keywords, key, default, fun)
934934
when is_list(keywords) and is_atom(key) and is_function(fun, 1) do
935935
update_guarded(keywords, key, default, fun)

lib/elixir/lib/map.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ defmodule Map do
619619
%{a: 1, b: 11}
620620
621621
"""
622-
@spec update(map, key, default :: value, (existing_value :: value -> updated_value :: value)) ::
622+
@spec update(map, key, default :: value, (existing_value :: value -> new_value :: value)) ::
623623
map
624624
def update(map, key, default, fun) when is_function(fun, 1) do
625625
case map do
@@ -817,7 +817,7 @@ defmodule Map do
817817
** (KeyError) key :b not found in: %{a: 1}
818818
819819
"""
820-
@spec update!(map, key, (existing_value :: value -> updated_value :: value)) :: map
820+
@spec update!(map, key, (existing_value :: value -> new_value :: value)) :: map
821821
def update!(map, key, fun) when is_function(fun, 1) do
822822
value = fetch!(map, key)
823823
put(map, key, fun.(value))
@@ -855,8 +855,8 @@ defmodule Map do
855855
{nil, %{a: 1}}
856856
857857
"""
858-
@spec get_and_update(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
859-
{current_value, map}
858+
@spec get_and_update(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
859+
{current_value, new_map :: map}
860860
when current_value: value
861861
def get_and_update(map, key, fun) when is_function(fun, 1) do
862862
current = get(map, key)
@@ -897,7 +897,7 @@ defmodule Map do
897897
{1, %{}}
898898
899899
"""
900-
@spec get_and_update!(map, key, (value -> {current_value, new_value :: value} | :pop)) ::
900+
@spec get_and_update!(map, key, (value | nil -> {current_value, new_value :: value} | :pop)) ::
901901
{current_value, map}
902902
when current_value: value
903903
def get_and_update!(map, key, fun) when is_function(fun, 1) do

0 commit comments

Comments
 (0)