You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to clarify a few things:
- module attributes are not constants
- Point out ExUnit's use of the `@tag` module attribute a bit earlier.
- I wanted to point out the "accumulate" option
- I wanted to see an example of calling functions when a module attribute is defined.
Copy file name to clipboardExpand all lines: getting-started/module-attributes.markdown
+29-5
Original file line number
Diff line number
Diff line change
@@ -79,7 +79,7 @@ This section covers built-in attributes. However, attributes can also be used by
79
79
80
80
## As "constants"
81
81
82
-
Elixir developers will often use module attributes as constants:
82
+
Elixir developers often use module attributes when they wish to make a value more visible or reusable:
83
83
84
84
```elixir
85
85
defmoduleMyServerdo
@@ -99,7 +99,7 @@ end
99
99
warning: undefined module attribute @unknown, please remove access to @unknownor explicitly set it before access
100
100
```
101
101
102
-
Finally, attributes can also be read inside functions:
102
+
Attributes can also be read inside functions:
103
103
104
104
```elixir
105
105
defmoduleMyServerdo
@@ -113,17 +113,41 @@ MyServer.first_data #=> 14
113
113
MyServer.second_data#=> 13
114
114
```
115
115
116
-
Every time an attribute is read inside a function, a snapshot of its current value is taken. In other words, the value is read at compilation time and not at runtime. As we are going to see, this also makes attributes useful to be used as storage during module compilation.
116
+
This is exactly how [ExUnit](https://hexdocs.pm/ex_unit/ExUnit.Case.html#module-tags) uses the `@tag` attribute to annotate tests (see the "temporary storage" section below for an example).
117
117
118
-
Any functions may be called when defining a module attribute. However, please note that functions you define in the same module as the attribute itself cannot be called, as they are not yet compiled at the time that the attribute is defined.
118
+
Every time an attribute is read inside a function, a snapshot of its current value is taken. In other words, the value is read at compilation time and not at runtime. As we are going to see, this also makes attributes useful as storage during module compilation.
119
+
120
+
Normally, repeating a module attribute will cause its value to be reassigned, but there are circumstances where you may want to [configure the module attribute](https://hexdocs.pm/elixir/Module.html#register_attribute/3) so that its values are accumulated:
Be careful, however: *functions defined in the same module as the attribute itself cannot be called* because they have not yet been compiled when the attribute is being defined.
119
143
120
144
When defining an attribute, do not leave a line break between the attribute name and its value.
121
145
122
146
## As temporary storage
123
147
124
148
One of the projects in the Elixir organization is [the `Plug` project](https://github.com/elixir-lang/plug), which is meant to be a common foundation for building web libraries and frameworks in Elixir.
125
149
126
-
The Plug library also allows developers to define their own plugs which can be run in a web server:
150
+
The Plug library allows developers to define their own plugs which can be run in a web server:
0 commit comments