Skip to content

Commit a629fda

Browse files
author
José Valim
committed
Document the record access rewrite feature
1 parent 3c7cc9a commit a629fda

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,39 @@ defmodule Kernel do
13831383
13841384
file_info.update_accesses(&1 + 1)
13851385
1386+
## Access syntax
1387+
1388+
Records in Elixir can be expanded at compilation time to provide
1389+
pattern matching and faster operations. For example, the clause
1390+
below will only match if a `FileInfo` is given and the number of
1391+
accesses is zero:
1392+
1393+
def enforce_no_access(FileInfo[accesses: 0]), do: :ok
1394+
1395+
The clause above will expand to:
1396+
1397+
def enforce_no_access({ FileInfo, _, 0 }), do: :ok
1398+
1399+
The downside of using such syntax is that, every time the record
1400+
changes, your code now needs to be recompiled (which is usually
1401+
not a concern since Elixir build tools by default recompiles the
1402+
whole project whenever there is a change).
1403+
1404+
Finally, keep in mind that Elixir triggers some optimizations whenever
1405+
the access syntax is used. For example:
1406+
1407+
def no_access?(FileInfo[] = file_info) do
1408+
file_info.accesses == 0
1409+
end
1410+
1411+
Is translated to:
1412+
1413+
def no_access?({ FileInfo, _, _ } = file_info) do
1414+
elem(file_info, 1) == 0
1415+
end
1416+
1417+
Which provides faster get and set times for record operations.
1418+
13861419
## Documentation
13871420
13881421
By default records are not documented and have `@moduledoc` set to false.
@@ -2665,17 +2698,18 @@ defmodule Kernel do
26652698
26662699
Enum.map(List.flatten([1,[2],3]), &1 * 2)
26672700
2668-
Please note that due to theoperator precendence you can't use
2669-
the following expression:
2701+
Please be aware of operator precendence, when using
2702+
this operator. For example, the following expression:
26702703
26712704
String.graphemes "Hello" /> Enum.reverse
26722705
2673-
as it is impossible to figure out whether /> is being applied
2674-
to "Hello" or String.graphemes/1. In the above case,
2675-
/> will be applied to "Hello", which will result in an error
2676-
as Enum.Iterator protocol is not defined for binaries.
2706+
Is translated to:
2707+
2708+
String.graphemes("Hello" /> Enum.reverse)
26772709
2678-
Therefore, the syntax that should be used is:
2710+
Which will result in an error as Enum.Iterator protocol
2711+
is not defined for binaries. Adding explicit parenthesis
2712+
is recommended:
26792713
26802714
String.graphemes("Hello") /> Enum.reverse
26812715

lib/elixir/src/elixir_module.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ functions_form(Line, File, Module, Export, Private, Def, Defmacro, Defmacrop, Ra
155155
record_rewrite_functions(Functions) ->
156156
lists:map(fun
157157
({ function, Line, Name, Arity, Clauses }) ->
158-
Rewriten = lists:map(fun(Clause) ->
158+
Rewriten = [begin
159159
{ C, _, _ } = 'Elixir.Kernel.RecordRewriter':optimize_clause(Clause),
160160
C
161-
end, Clauses),
161+
end || Clause <- Clauses],
162162
{ function, Line, Name, Arity, Rewriten };
163163
(Other) -> Other
164164
end, Functions).

0 commit comments

Comments
 (0)