@@ -36,35 +36,43 @@ defmodule Access do
3636 @ type key :: any
3737 @ type value :: any
3838
39- @ callback get ( t , key , value ) :: value
39+ @ callback fetch ( t , key ) :: { :ok , value } | :error
4040 @ callback get_and_update ( t , key , ( value -> { value , value } ) ) :: { value , t }
4141
4242 @ doc """
43- Gets the container's value for the given key.
43+ Fetches the container's value for the given key.
4444 """
45- @ spec get ( t , term , term ) :: term
46- def get ( container , key , default \\ nil )
45+ @ spec fetch ( t , term ) :: { :ok , term } | :error
46+ def fetch ( container , key )
4747
48- def get ( % { __struct__: struct } = container , key , default ) do
49- struct . get ( container , key , default )
48+ def fetch ( % { __struct__: struct } = container , key ) do
49+ struct . fetch ( container , key )
5050 end
5151
52- def get ( % { } = map , key , default ) do
53- case :maps . find ( key , map ) do
54- { :ok , value } -> value
55- :error -> default
56- end
52+ def fetch ( % { } = map , key ) do
53+ :maps . find ( key , map )
5754 end
5855
59- def get ( list , key , default ) when is_list ( list ) do
56+ def fetch ( list , key ) when is_list ( list ) do
6057 case :lists . keyfind ( key , 1 , list ) do
61- { ^ key , value } -> value
62- false -> default
58+ { ^ key , value } -> { :ok , value }
59+ false -> :error
6360 end
6461 end
6562
66- def get ( nil , _key , default ) do
67- default
63+ def fetch ( nil , _key ) do
64+ :error
65+ end
66+
67+ @ doc """
68+ Gets the container's value for the given key.
69+ """
70+ @ spec get ( t , term , term ) :: term
71+ def get ( container , key , default \\ nil ) do
72+ case fetch ( container , key ) do
73+ { :ok , value } -> value
74+ :error -> default
75+ end
6876 end
6977
7078 @ doc """
0 commit comments