Skip to content

Commit a15d4d2

Browse files
fireproofsocksJosé Valim
authored and
José Valim
committed
Update typespecs-and-behaviours.markdown (elixir-lang#1348)
1 parent 8065077 commit a15d4d2

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

getting-started/typespecs-and-behaviours.markdown

+15-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ The syntax is to put the function and its input on the left side of the `::` and
2626

2727
In code, function specs are written with the `@spec` attribute, typically placed immediately before the function definition. Specs can describe both public and private functions. The function name and the number of arguments used in the `@spec` attribute must match the function it describes.
2828

29-
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](https://hexdocs.pm/elixir/typespecs.html).
29+
Elixir supports compound types as well. For example, a list of integers has type `[integer]`, or maps that define keys and types (see the example below).
30+
31+
You can see all the built-in types provided by Elixir [in the typespecs docs](https://hexdocs.pm/elixir/typespecs.html).
3032

3133
### Defining custom types
3234

@@ -48,6 +50,17 @@ end
4850

4951
The `@typedoc` attribute, similar to the `@doc` and `@moduledoc` attributes, is used to document custom types.
5052

53+
You may define compound custom types, e.g. maps:
54+
55+
```elixir
56+
@type error_map :: %{
57+
message: String.t,
58+
line_number: integer
59+
}
60+
```
61+
62+
[Structs](https://elixir-lang.org/getting-started/structs.html) offer similar functionalty.
63+
5164
Let's look at another example to understand how to define more complex types. 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.
5265

5366
```elixir
@@ -91,7 +104,7 @@ defmodule QuietCalculator do
91104
end
92105
```
93106

94-
If you want to keep a custom type private, you can use the `@typep` attribute instead of `@type`. The visibility also affects whether or not documentation will be generated by toolks like [ExDoc](https://hexdocs.pm/ex_doc/readme.html), Elixir's documentation generator.
107+
If you want to keep a custom type private, you can use the `@typep` attribute instead of `@type`. The visibility also affects whether or not documentation will be generated by tools like [ExDoc](https://hexdocs.pm/ex_doc/readme.html), Elixir's documentation generator.
95108

96109
### Static code analysis
97110

0 commit comments

Comments
 (0)