Skip to content

Commit

Permalink
Deprecate Keyword.replace/3 and Map.replace/3
Browse files Browse the repository at this point in the history
`replace!` was introduced to mirror `update!` but without the
anonymous function. Given there is no `update`, we should not
introduce a `replace` until we are sure of the semantics we
want for update.
  • Loading branch information
José Valim committed Oct 4, 2017
1 parent 37129c2 commit a81a17b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 31 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
#### Elixir

* [Enum] `Enum.partition/2` is deprecated in favor of `Enum.split_with/2`
* [Keyword] `Keyword.replace/3` is deprecated in favor of `Keyword.fetch/2` and `Keyword.put/3`
* [Map] `Map.replace/3` is deprecated in favor of `Map.fetch/2` and `Map.put/3`

## v1.5

Expand Down
18 changes: 1 addition & 17 deletions lib/elixir/lib/keyword.ex
Original file line number Diff line number Diff line change
Expand Up @@ -594,23 +594,7 @@ defmodule Keyword do
end
end

@doc """
Alters the value stored under `key` to `value`, but only
if the entry `key` already exists in the keyword list.
In the case a value is stored multiple times in the keyword list,
later occurrences are removed.
## Examples
iex> Keyword.replace([a: 1, b: 2, a: 4], :a, 3)
[a: 3, b: 2]
iex> Keyword.replace([a: 1], :b, 2)
[a: 1]
"""
@spec replace(t, key, value) :: t
@doc false
def replace(keywords, key, value) when is_list(keywords) and is_atom(key) do
case :lists.keyfind(key, 1, keywords) do
{^key, _} -> [{key, value} | delete(keywords, key)]
Expand Down
15 changes: 1 addition & 14 deletions lib/elixir/lib/map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,7 @@ defmodule Map do
end
end

@doc """
Alters the value stored under `key` to `value`, but only
if the entry `key` already exists in `map`.
## Examples
iex> Map.replace(%{a: 1, b: 2}, :a, 3)
%{a: 3, b: 2}
iex> Map.replace(%{a: 1}, :b, 2)
%{a: 1}
"""
@spec replace(map, key, value) :: map
@doc false
def replace(map, key, value) do
case map do
%{^key => _value} ->
Expand Down
2 changes: 2 additions & 0 deletions lib/elixir/pages/Deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Elixir deprecations happen in 3 steps:

Deprecated feature | Deprecated in | Replaced by (available since)
:----------------------------------------------- | :------------ | :----------------------------
`Keyword.replace/3` | [v1.6] | Use `Keyword.fetch/2` + `Keyword.put/3` (v1.0)
`Map.replace/3` | [v1.6] | Use `Map.fetch/2` + `Map.put/3` (v1.0)
`Macro.unescape_tokens/1` and `Macro.unescape_tokens/2` | [v1.6] | Use `Enum.map/2` to traverse over the arguments (v1.0)
`Enum.partition/2` | [v1.6] | `Enum.split_with/2` (v1.4)
`Atom.to_char_list/1` | [v1.5] | `Atom.to_charlist/1` (v1.3)
Expand Down
4 changes: 4 additions & 0 deletions lib/elixir/src/elixir_dispatch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,14 @@ deprecation('Elixir.Integer', to_char_list, 2) ->
"use Integer.to_charlist/2";
deprecation('Elixir.Kernel', to_char_list, 1) ->
"use Kernel.to_charlist/1";
deprecation('Elixir.Keyword', replace, 3) ->
"use Keyword.fetch/2 + Keyword.put/3";
deprecation('Elixir.Keyword', size, 1) ->
"use Kernel.length/1";
deprecation('Elixir.List.Chars', to_char_list, 1) ->
"use List.Chars.to_charlist/1";
deprecation('Elixir.Map', replace, 3) ->
"use Map.fetch/2 + Map.put/3";
deprecation('Elixir.Map', size, 1) ->
"use Kernel.map_size/1";
deprecation('Elixir.Macro', unescape_tokens, 1) ->
Expand Down

0 comments on commit a81a17b

Please sign in to comment.