Using of bracket syntax in Elixir record
elixir
Solution
Very good question!
The difference is the context. In the first example, Elixir knows `FileInfo[]` is being invoked inside a function signature (in particular, inside a match context), and therefore, instead of using the default values, it expands all non-given values to `_`.
In the other example, we are not in a match context, it is just the regular context. In this context, `_` is not even valid:
iex> _
** (CompileError) iex:1: unbound variable _
In this case, Elixir expands the non-given values to the default ones.
Problem
I'm learning the Elixir programming with elixir lang getting started , and I'm stacked of the `record brace syntax`. This is the sample: ``` defrecord FileInfo, atime: nil, accesses: 0 defmodule FileAccess do def was_accessed?(FileInfo[accesses: 0]), do: false def was_accessed?(FileInfo[]), do: true end ``` While the author consider Elixir expands the record to a tuple at compilation time. so ``` def was_accessed?(FileInfo[accesses: 0]), do: false ``` is the same as: ``` def was_accessed?({ FileInfo, _, 0 }), do: false ``` But when I type in the Elixir shell: ``` iex(13)> FileInfo[access: 0] == {FileInfo, nil, 0} true iex(14)> FileInfo[access: 0] == {FileInfo, 0, 2} false ``` The result turned out `FileInfo[access: 0]` only equals to `{FileInfo, nil, 0}`, not `{ FileInfo, _, 0 }`. What's the difference of these two scenes?