Skip to content

Commit 3f39699

Browse files
committed
Clean up the "Typespecs" page in the documentation
[ci skip]
1 parent 4155e18 commit 3f39699

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

lib/elixir/pages/Typespecs.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
# Typespecs
22

3-
Elixir comes with a notation for declaring types and specifications. Elixir is dynamically typed, and as such, typespecs are never used by the compiler to optimize or modify code. Still, using typespecs is useful as documentation and tools such as [Dialyzer](http://www.erlang.org/doc/man/dialyzer.html) can analyze code with typespecs to find bugs.
3+
Elixir comes with a notation for declaring types and specifications. Elixir is a dynamically typed language, and as such, type specifications are never used by the compiler to optimize or modify code. Still, using type specifications is useful because
44

5-
The attributes `@type`, `@opaque`, `@typep`, `@spec`, `@callback` and `@macrocallback` are the main mechanism for defining typespecs. See sub-sections "Defining a type" and "Defining a specification" below.
5+
* they provide documentation (for example, tools such as [ExDoc](https://github.com/elixir-lang/ex_doc) show type specifications in the documentation)
6+
* they're used by tools sych as [Dialyzer](http://www.erlang.org/doc/man/dialyzer.html), that can analyze code with typespec to find type inconsistencies and possible bugs
67

7-
## Types and their syntax
8+
Type specifications (sometimes referred to as *typespecs*) are defined in different contexts using the following attributes:
9+
10+
* `@type`
11+
* `@opaque`
12+
* `@typep`
13+
* `@spec`
14+
* `@callback`
15+
* `@macrocallback`
816

9-
The type syntax provided by Elixir is fairly similar to [the one in Erlang](http://www.erlang.org/doc/reference_manual/typespec.html).
17+
See the "Defining a type" and "Defining a specification" sub-sections below for more information on defining types and typespecs.
1018

11-
Most of the built-in types provided in Erlang (for example, `pid()`) are expressed the same way: `pid()` or simply `pid`. Parameterized types are also supported (`list(integer)`) and so are remote types (`Enum.t`).
19+
## Types and their syntax
1220

13-
Integers and atom literals are allowed as types (ex. `1`, `:atom` or `false`). All other types are built of unions of predefined types. Certain shorthands are allowed, such as `[...]`, `<<>>` and `{...}`.
21+
The syntax Elixir provides for type specifications is similar to [the one in Erlang](http://www.erlang.org/doc/reference_manual/typespec.html). Most of the built-in types provided in Erlang (for example, `pid()`) are expressed in the same way: `pid()` (or simply `pid`). Parametrized types (such as `list(integer)`) are supported as well and so are remote types (such as `Enum.t`). Integers and atom literals are allowed as types (e.g., `1`, `:atom`, or `false`). All other types are built out of unions of predefined types. Some shorthands are allowed, such as `[...]`, `<<>>`, and `{...}`.
1422

1523
### Basic types
1624

@@ -78,7 +86,7 @@ The following literals are also supported in typespecs:
7886

7987
### Built-in types
8088

81-
These types are also provided by Elixir as shortcuts on top of the basic and literal types.
89+
The following types are also provided by Elixir as shortcuts on top of the basic and literal types described above.
8290

8391
Built-in type | Defined as
8492
:---------------------- | :---------
@@ -110,7 +118,7 @@ Built-in type | Defined as
110118

111119
### Remote types
112120

113-
Any module is also able to define its own type and the modules in Elixir are no exception. For example, a string is `String.t`, a range is `Range.t`, any enumerable can be `Enum.t` and so on.
121+
Any module is also able to define its own types and the modules in Elixir are no exception. For example, the `Range` module defines a `t` type that represents a range: this type can be referred to as `Range.t`. In a similar fashion, a string is `String.t`, any enumerable can be `Enum.t`, and so on.
114122

115123
### Maps
116124

@@ -122,13 +130,15 @@ Notice that the syntactic representation of `map()` is `%{...}` (or `%{optional(
122130

123131
## Defining a type
124132

133+
The `@type`, `@typep`, and `@opaque` module attributes can be used to define new types:
134+
125135
@type type_name :: type
126136
@typep type_name :: type
127137
@opaque type_name :: type
128138

129139
A type defined with `@typep` is private. An opaque type, defined with `@opaque` is a type where the internal structure of the type will not be visible, but the type is still public.
130140

131-
Types can be parameterized by defining variables as parameters, these variables can then be used to define the type.
141+
Types can be parameterized by defining variables as parameters; these variables can then be used to define the type.
132142

133143
@type dict(key, value) :: [{key, value}]
134144

@@ -148,17 +158,15 @@ Type variables with no restriction can also be defined.
148158

149159
@spec function(arg) :: [arg] when arg: var
150160

151-
You can also name your arguments in a typespec using `arg_name :: arg_type` syntax.
152-
This is particularly useful as a way to differentiate multiple arguments
153-
of the same type (or multiple elements of the same type in a type def):
161+
You can also name your arguments in a typespec using `arg_name :: arg_type` syntax. This is particularly useful in documentation as a way to differentiate multiple arguments of the same type (or multiple elements of the same type in a type definition):
154162

155163
@spec days_since_epoch(year :: integer, month :: integer, day :: integer) :: integer
156164
@type color :: {red :: integer, green :: integer, blue :: integer}
157165

158166
Specifications can be overloaded just like ordinary functions.
159167

160168
@spec function(integer) :: atom
161-
@spec function(atom) :: integer
169+
@spec function(atom) :: integer
162170

163171
## Notes
164172

0 commit comments

Comments
 (0)