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
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.
60
+
像这样定义函数规格说明是没问题,但是一次次重复写这种复合类型的
61
+
样式名称`{number, String.t}`,很快会厌烦。
62
+
我们可以使用`@type`指令来声明我们自定义的类型:
61
63
62
64
```elixir
63
65
defmoduleLousyCalculatordo
@@ -74,9 +76,9 @@ defmodule LousyCalculator do
74
76
end
75
77
```
76
78
77
-
The `@typedoc` directive, similarly to the `@doc` and `@moduledoc` directives, is used to document custom types.
79
+
指令`@typedoc`,和`@doc`或`@moduledoc`指令类似,被用来记录自定义类型。
78
80
79
-
Custom types defined through `@type` are exported and available outside the module they're defined in:
81
+
自定义类型通过`@type`定义,可以从其定义的模块导出并被外界访问:
80
82
81
83
```elixir
82
84
defmoduleQuietCalculatordo
@@ -88,11 +90,15 @@ defmodule QuietCalculator do
88
90
end
89
91
```
90
92
91
-
If you want to keep a custom type private, you can use the `@typep`directive instead of `@type`.
93
+
如果想要将某个自定义类型保持私有,可以使用 `@typep`指令代替 `@type` 。
92
94
93
-
### Static code analysis
95
+
### 静态代码分析
94
96
95
-
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