Skip to content

Commit 8e65993

Browse files
author
José Valim
committed
Merge pull request #1417 from jwarwick/issue_1414
Fix for Issue 1414, Record.__record__(:index) syntax
2 parents 095a91a + 82da86a commit 8e65993

File tree

6 files changed

+35
-7
lines changed

6 files changed

+35
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* [Enum] Receiving the index of iteration in `Enum.map/2` and `Enum.each/2` is deprecated in favor of `Stream.with_index/1`
2626
* [File] `File.iterator/1` and `File.biniterator/1` are deprecated in favor of `IO.stream/1` and `IO.binstream/1`
2727
* [File] `File.iterator!/2` and `File.biniterator!/2` are deprecated in favor of `File.stream!/2` and `File.binstream!/2`
28+
* [Record] `Record.__index__/2` deprecated in favor of `Record.__record__(:index, key)`
2829

2930
* backwards incompatible changes
3031
* [Kernel] The `Binary.Inspect` protocol has been renamed to `Inspect`

lib/elixir/lib/kernel.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,12 +1517,12 @@ defmodule Kernel do
15171517
#=> [name: nil, age: 0]
15181518
15191519
In order to quickly access the index of a field, one can use
1520-
the `__index__` function:
1520+
the `__record__` function with `:index` as the first argument:
15211521
1522-
User.__index__(:age)
1522+
User.__record__(:index, :age)
15231523
#=> 2
15241524
1525-
User.__index__(:unknown)
1525+
User.__record__(:index, :unknown)
15261526
#=> nil
15271527
15281528
## Compile-time introspection

lib/elixir/lib/record.ex

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ defmodule Record do
573573
## Function generation
574574
575575
# Define __record__/1 and __record__/2 as reflection functions
576-
# that returns the record names and fields.
576+
# 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
@@ -585,9 +585,26 @@ defmodule Record do
585585
#
586586
# FileInfo.__record__(:name) #=> FileInfo
587587
# FileInfo.__record__(:fields) #=> [atime: nil, mtime: nil]
588+
# FileInfo.__record__(:index, :atime) #=> 1
589+
# FileInfo.__record__(:index, :mtime) #=> 2
588590
#
589591
defp reflection(values) do
592+
quoted = lc { k, _ } inlist values do
593+
index = find_index(values, k, 0)
594+
quote do
595+
@doc false
596+
def __record__(:index, unquote(k)), do: unquote(index + 1)
597+
end
598+
end
599+
590600
quote do
601+
unquote(quoted)
602+
603+
@doc false
604+
def __record__(:index, _), do: nil
605+
@doc false
606+
def __record__(:index, arg, _), do: __record__(:index, arg)
607+
591608
@doc false
592609
def __record__(kind, _), do: __record__(kind)
593610

lib/elixir/lib/system.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ defmodule System do
162162
defp write_tmp_dir(dir) do
163163
case :file.read_file_info(dir) do
164164
{:ok, info} ->
165-
type_index = File.Stat.__index__ :type
166-
access_index = File.Stat.__index__ :access
165+
type_index = File.Stat.__record__(:index, :type)
166+
access_index = File.Stat.__record__(:index, :access)
167167
case { elem(info, type_index), elem(info, access_index) } do
168168
{ :directory, access } when access in [:read_write, :write] ->
169169
dir

lib/elixir/test/elixir/record_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ defmodule RecordTest do
126126
assert RecordTest.FileInfo.__index__(:atime) == record.__index__(:atime)
127127
end
128128

129+
test :__record_index__ do
130+
record = RecordTest.DynamicName.new(a: "a", b: "b")
131+
assert record.__record__(:index, :a) == 1
132+
assert elem(record, record.__record__(:index, :a)) == "a"
133+
assert elem(record, record.__record__(:index, :b)) == "b"
134+
assert record.__record__(:index, :c) == nil
135+
record = RecordTest.FileInfo.new
136+
assert RecordTest.FileInfo.__record__(:index, :atime) == record.__record__(:index, :atime)
137+
end
138+
129139
test :to_keywords do
130140
record = RecordTest.DynamicName.new(a: "a", b: "b")
131141
assert record.to_keywords[:a] == "a"

lib/mix/lib/mix/deps.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ defmodule Mix.Deps do
8282
deps = Enum.filter all_deps, fn(dep) -> dep.app in apps end
8383

8484
# Now we validate the given atoms
85-
index = Mix.Dep.__index__(:app)
85+
index = Mix.Dep.__record__(:index, :app)
8686
Enum.each apps, fn(app) ->
8787
unless List.keyfind(deps, app, index) do
8888
Mix.shell.info "unknown dependency #{app} for env #{Mix.env}"

0 commit comments

Comments
 (0)