Elixir v1.0.5 Access protocol
The Access protocol is used by foo[bar]
and also
empowers the nested update functions in Kernel.
For instance, foo[bar]
translates Access.get(foo, bar)
.
Kernel.get_in/2
, Kernel.put_in/3
, Kernel.update_in/3
and
Kernel.get_and_update_in/3
are also all powered by the Access
protocol.
This protocol is implemented by default for keywords, maps and dictionary like types:
iex> keywords = [a: 1, b: 2]
iex> keywords[:a]
1
iex> map = %{a: 1, b: 2}
iex> map[:a]
1
iex> star_ratings = %{1.0 => "★", 1.5 => "★☆", 2.0 => "★★"}
iex> star_ratings[1.5]
"★☆"
The key comparison must be implemented using the ===
operator.
Summary
Functions
Accesses the given key in the container
Gets a value and updates the given key
in one pass
Types
t :: term
Functions
Gets a value and updates the given key
in one pass.
The function must receive the value for the given key
(or nil
if the key doesn’t exist in container
) and
the function must return a tuple containing the get
value and the new value to be stored in the container
.