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
Copy file name to clipboardExpand all lines: getting-started/module-attributes.markdown
+1-7
Original file line number
Diff line number
Diff line change
@@ -74,13 +74,7 @@ iex> h Math.sum # Access the docs for the sum function
74
74
75
75
We also provide a tool called [ExDoc](https://github.com/elixir-lang/ex_doc) which is used to generate HTML pages from the documentation.
76
76
77
-
You can take a look at the docs for [Module](/docs/stable/elixir/Module.html) for a complete list of supported attributes. Elixir also uses attributes to define [typespecs](/docs/stable/elixir/Kernel.Typespec.html), via:
78
-
79
-
*`@spec` - provides a specification for a function.
80
-
*`@callback` - provides a specification for the behaviour callback.
81
-
*`@type` - defines a type to be used in `@spec`.
82
-
*`@typep` - defines a private type to be used in `@spec`.
83
-
*`@opaque` - defines an opaque type to be used in `@spec`.
77
+
You can take a look at the docs for [Module](/docs/stable/elixir/Module.html) for a complete list of supported attributes. Elixir also uses attributes to define [typespecs](/docs/stable/elixir/typespecs.html).
84
78
85
79
This section covers built-in attributes. However, attributes can also be used by developers or extended by libraries to support custom behaviour.
Copy file name to clipboardExpand all lines: getting-started/typespecs-and-behaviours.markdown
+13-13
Original file line number
Diff line number
Diff line change
@@ -30,13 +30,13 @@ round(number) :: integer
30
30
defround(number), do:# implementation...
31
31
```
32
32
33
-
Elixir supports compound types as well. For example, a list of integers has type `[integer]`. You can see all the types provided by Elixir [in the typespecs docs](/docs/stable/elixir/Kernel.Typespec.html).
33
+
Elixir supports compound types as well. For example, a list of integers has type `[integer]`. You can see all the built-in types provided by Elixir [in the typespecs docs](/docs/stable/elixir/typespecs.html).
34
34
35
35
### Defining custom types
36
36
37
37
While Elixir provides a lot of useful built-in types, it's convenient to define custom types when appropriate. This can be done when defining modules through the `@type` directive.
38
38
39
-
Say we have a `LousyCalculator` module, which performs the usual arithmetic operations (sum, product and so on) but, instead of returning numbers, it returns tuples with the result of an operation as the first element and a random offense as the second element.
39
+
Say we have a `LousyCalculator` module, which performs the usual arithmetic operations (sum, product and so on) but, instead of returning numbers, it returns tuples with the result of an operation as the first element and a random remark as the second element.
40
40
41
41
```elixir
42
42
defmoduleLousyCalculatordo
@@ -48,7 +48,7 @@ defmodule LousyCalculator do
48
48
end
49
49
```
50
50
51
-
As you can see in the example, tuples are a compound type and each tuple is identified by the types inside it. To understand why `String.t` is not written as `string`, have another look at the [typespecs docs](/docs/stable/elixir/Kernel.Typespec.html).
51
+
As you can see in the example, tuples are a compound type and each tuple is identified by the types inside it. To understand why `String.t` is not written as `string`, have another look at the [notes in the typespecs docs](/docs/stable/elixir/typespecs.html#Notes).
52
52
53
53
Defining function specs this way works, but it quickly becomes annoying since we're repeating the type `{number, String.t}` over and over. We can use the `@type` directive in order to declare our own custom type.
54
54
@@ -57,13 +57,13 @@ defmodule LousyCalculator do
57
57
@typedoc """
58
58
Just a number followed by a string.
59
59
"""
60
-
@typenumber_with_offense:: {number, String.t}
60
+
@typenumber_with_remark:: {number, String.t}
61
61
62
-
@specadd(number, number) ::number_with_offense
63
-
defadd(x, y), do: {x + y, "You need a calculator to do that?!"}
62
+
@specadd(number, number) ::number_with_remark
63
+
defadd(x, y), do: {x + y, "You need a calculator to do that?"}
@specmake_polite(LousyCalculator.number_with_offense) :: number
80
-
defpmake_polite({num, _offense}), do: num
79
+
@specmake_quiet(LousyCalculator.number_with_remark) :: number
80
+
defpmake_quiet({num, _remark}), do: num
81
81
end
82
82
```
83
83
84
84
If you want to keep a custom type private, you can use the `@typep` directive instead of `@type`.
85
85
86
86
### Static code analysis
87
87
88
-
Typespecs are not only useful to developers and as additional documentation. The Erlang tool [Dialyzer](http://www.erlang.org/doc/man/dialyzer.html), for example, uses typespecs in order to perform static analysis of code. That's why, in the `PoliteCalculator` example, we wrote a spec for the `make_polite/1` function even if it was defined as a private function.
88
+
Typespecs are not only useful to developers and as additional documentation. The Erlang tool [Dialyzer](http://www.erlang.org/doc/man/dialyzer.html), for example, uses typespecs in order to perform static analysis of code. That's why, in the `QuietCalculator` example, we wrote a spec for the `make_quiet/1` function even if it was defined as a private function.
0 commit comments