@@ -1383,6 +1383,39 @@ defmodule Kernel do
1383
1383
1384
1384
file_info.update_accesses(&1 + 1)
1385
1385
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
+
1386
1419
## Documentation
1387
1420
1388
1421
By default records are not documented and have `@moduledoc` set to false.
@@ -2665,17 +2698,18 @@ defmodule Kernel do
2665
2698
2666
2699
Enum.map(List.flatten([1,[2],3]), &1 * 2)
2667
2700
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:
2670
2703
2671
2704
String.graphemes "Hello" /> Enum.reverse
2672
2705
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)
2677
2709
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:
2679
2713
2680
2714
String.graphemes("Hello") /> Enum.reverse
2681
2715
0 commit comments