Skip to content

Commit a69ab67

Browse files
author
José Valim
committed
Point to the typespec page
1 parent cde4adf commit a69ab67

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

Diff for: getting-started/module-attributes.markdown

+1-7
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,7 @@ iex> h Math.sum # Access the docs for the sum function
7474

7575
We also provide a tool called [ExDoc](https://github.com/elixir-lang/ex_doc) which is used to generate HTML pages from the documentation.
7676

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).
8478

8579
This section covers built-in attributes. However, attributes can also be used by developers or extended by libraries to support custom behaviour.
8680

Diff for: getting-started/typespecs-and-behaviours.markdown

+13-13
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ round(number) :: integer
3030
def round(number), do: # implementation...
3131
```
3232

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).
3434

3535
### Defining custom types
3636

3737
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.
3838

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.
4040

4141
```elixir
4242
defmodule LousyCalculator do
@@ -48,7 +48,7 @@ defmodule LousyCalculator do
4848
end
4949
```
5050

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).
5252

5353
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.
5454

@@ -57,13 +57,13 @@ defmodule LousyCalculator do
5757
@typedoc """
5858
Just a number followed by a string.
5959
"""
60-
@type number_with_offense :: {number, String.t}
60+
@type number_with_remark :: {number, String.t}
6161

62-
@spec add(number, number) :: number_with_offense
63-
def add(x, y), do: {x + y, "You need a calculator to do that?!"}
62+
@spec add(number, number) :: number_with_remark
63+
def add(x, y), do: {x + y, "You need a calculator to do that?"}
6464

65-
@spec multiply(number, number) :: number_with_offense
66-
def multiply(x, y), do: {x * y, "Jeez, come on!"}
65+
@spec multiply(number, number) :: number_with_remark
66+
def multiply(x, y), do: {x * y, "It is like addition on steroids."}
6767
end
6868
```
6969

@@ -72,20 +72,20 @@ The `@typedoc` directive, similarly to the `@doc` and `@moduledoc` directives, i
7272
Custom types defined through `@type` are exported and available outside the module they're defined in:
7373

7474
```elixir
75-
defmodule PoliteCalculator do
75+
defmodule QuietCalculator do
7676
@spec add(number, number) :: number
77-
def add(x, y), do: make_polite(LousyCalculator.add(x, y))
77+
def add(x, y), do: make_quiet(LousyCalculator.add(x, y))
7878

79-
@spec make_polite(LousyCalculator.number_with_offense) :: number
80-
defp make_polite({num, _offense}), do: num
79+
@spec make_quiet(LousyCalculator.number_with_remark) :: number
80+
defp make_quiet({num, _remark}), do: num
8181
end
8282
```
8383

8484
If you want to keep a custom type private, you can use the `@typep` directive instead of `@type`.
8585

8686
### Static code analysis
8787

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.
8989

9090
## Behaviours
9191

0 commit comments

Comments
 (0)