Skip to content

Commit 73b9ac7

Browse files
author
José Valim
committed
Add deprecation warning to __index__ on Records
1 parent 8e65993 commit 73b9ac7

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

lib/elixir/lib/inspect.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ defimpl Inspect, for: List do
215215
cond do
216216
:io_lib.printable_list(thing) ->
217217
<< ?', String.escape(:unicode.characters_to_binary(thing), ?') :: binary, ?' >>
218-
keyword?(thing) && !opts.raw ->
218+
keyword?(thing) && not opts.raw ->
219219
surround_many("[", thing, "]", opts.limit, keyword(&1, opts))
220220
true ->
221221
surround_many("[", thing, "]", opts.limit, Kernel.inspect(&1, opts))

lib/elixir/lib/record.ex

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,8 @@ defmodule Record do
572572
573573
## Function generation
574574
575-
# Define __record__/1 and __record__/2 as reflection functions
576-
# that return the record names and fields.
575+
# Define __record__/1, __record__/2, __record__/3 as reflection
576+
# functions that return the record names and fields.
577577
#
578578
# Note that fields are *not* keywords. They are in the same
579579
# order as given as parameter and reflects the order of the
@@ -592,7 +592,6 @@ defmodule Record do
592592
quoted = lc { k, _ } inlist values do
593593
index = find_index(values, k, 0)
594594
quote do
595-
@doc false
596595
def __record__(:index, unquote(k)), do: unquote(index + 1)
597596
end
598597
end
@@ -606,11 +605,11 @@ defmodule Record do
606605
def __record__(:index, arg, _), do: __record__(:index, arg)
607606
608607
@doc false
609-
def __record__(kind, _), do: __record__(kind)
608+
def __record__(kind, _), do: __record__(kind)
610609
611610
@doc false
612-
def __record__(:name), do: __MODULE__
613-
def __record__(:fields), do: unquote(values)
611+
def __record__(:name), do: __MODULE__
612+
def __record__(:fields), do: unquote(values)
614613
end
615614
end
616615
@@ -675,7 +674,11 @@ defmodule Record do
675674
index = find_index(values, k, 0)
676675
quote do
677676
@doc false
678-
def __index__(unquote(k)), do: unquote(index + 1)
677+
def __index__(unquote(k)) do
678+
IO.write "[WARNING] #{__MODULE__}.__index__/1 is deprecated, " <>
679+
"please use #{__MODULE__}.__record__(:index, key) instead\n#{Exception.format_stacktrace}"
680+
unquote(index + 1)
681+
end
679682
end
680683
end
681684
quote do
@@ -832,7 +835,7 @@ defmodule Record do
832835
@spec update(options, t) :: t
833836
@spec __record__(:name) :: atom
834837
@spec __record__(:fields) :: [{atom, any}]
835-
@spec __index__(atom) :: non_neg_integer | nil
838+
@spec __record__(:index, atom) :: non_neg_integer | nil
836839
end
837840
end
838841

lib/elixir/test/elixir/record_test.exs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,6 @@ defmodule RecordTest do
117117
refute is_record(RecordTest.FileInfo.new, List)
118118
end
119119

120-
test :__index__ do
121-
record = RecordTest.DynamicName.new(a: "a", b: "b")
122-
assert elem(record, record.__index__(:a)) == "a"
123-
assert elem(record, record.__index__(:b)) == "b"
124-
assert record.__index__(:c) == nil
125-
record = RecordTest.FileInfo.new
126-
assert RecordTest.FileInfo.__index__(:atime) == record.__index__(:atime)
127-
end
128-
129120
test :__record_index__ do
130121
record = RecordTest.DynamicName.new(a: "a", b: "b")
131122
assert record.__record__(:index, :a) == 1

0 commit comments

Comments
 (0)