Skip to content

Commit b3e8a3d

Browse files
author
José Valim
committed
Improve docs for records
1 parent 021dc54 commit b3e8a3d

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,26 @@ defmodule Kernel do
14311431
14321432
Which provides faster get and set times for record operations.
14331433
1434+
## Runtime introspection
1435+
1436+
At runtime, developers can use `__record__` to get information
1437+
about the given record:
1438+
1439+
FileInfo.__record__(:name)
1440+
#=> FileInfo
1441+
1442+
FileInfo.__record__(:fields)
1443+
#=> [atime: nil, accesses: 0]
1444+
1445+
In order to quickly access the index of a field, one can use
1446+
the `__index__` function:
1447+
1448+
FileInfo.__index__(:atime)
1449+
#=> 0
1450+
1451+
FileInfo.__index__(:unknown)
1452+
#=> nil
1453+
14341454
## Compile-time introspection
14351455
14361456
At the compile time, one can access following information about the record

lib/elixir/lib/record.ex

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ defmodule Record do
156156
end
157157

158158
contents = quote do
159-
160159
defmacrop unquote(name)() do
161160
Record.access(unquote(tag) || __MODULE__, unquote(escaped), [], __CALLER__)
162161
end
@@ -242,7 +241,8 @@ defmodule Record do
242241
{ match, remaining } = :lists.mapfoldl(iterator, keyword, fields)
243242

244243
case remaining do
245-
[] -> { :{}, [line: caller.line], [atom|match] }
244+
[] ->
245+
quote do: { unquote_splicing([atom|match]) }
246246
_ ->
247247
keys = lc { key, _ } inlist remaining, do: key
248248
raise "record #{inspect atom} does not have the keys: #{inspect keys}"
@@ -416,6 +416,7 @@ defmodule Record do
416416
quoted = lc { k, _ } inlist values do
417417
index = find_index(values, k, 0)
418418
quote do
419+
@doc false
419420
def __index__(unquote(k)), do: unquote(index + 1)
420421
end
421422
end
@@ -458,27 +459,27 @@ defmodule Record do
458459
#
459460
# It will define four methods:
460461
#
461-
# def :atime.(record) do
462+
# def atime(record) do
462463
# elem(record, 1)
463464
# end
464465
#
465-
# def :mtime.(record) do
466+
# def mtime(record) do
466467
# elem(record, 2)
467468
# end
468469
#
469-
# def :atime.(value, record) do
470+
# def atime(value, record) do
470471
# set_elem(record, 1, value)
471472
# end
472473
#
473-
# def :mtime.(record) do
474+
# def mtime(record) do
474475
# set_elem(record, 2, value)
475476
# end
476477
#
477-
# def :atime.(callback, record) do
478+
# def atime(callback, record) do
478479
# set_elem(record, 1, callback.(elem(record, 1)))
479480
# end
480481
#
481-
# def :mtime.(callback, record) do
482+
# def mtime(callback, record) do
482483
# set_elem(record, 2, callback.(elem(record, 2)))
483484
# end
484485
#
@@ -525,7 +526,7 @@ defmodule Record do
525526
end
526527
end
527528

528-
contents = { :{}, [], [(quote do: __MODULE__)|fields] }
529+
contents = quote do: { __MODULE__, unquote_splicing(fields) }
529530

530531
quote do
531532
@doc false
@@ -571,6 +572,8 @@ defmodule Record do
571572
@spec new(options | tuple) :: t
572573
@spec to_keywords(t) :: options
573574
@spec update(options, t) :: t
575+
@spec __record__(:name) :: atom
576+
@spec __record__(:fields) :: [{atom,any}]
574577
@spec __index__(atom) :: non_neg_integer | nil
575578
end
576579
end

0 commit comments

Comments
 (0)