Skip to content

Commit 0d7d755

Browse files
committed
[#18] specs
1 parent 1b1b103 commit 0d7d755

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

README.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,20 +261,9 @@ Erlang syntax is horrible amirite? So you might as well make the best of it, rig
261261
##### Write function specs
262262
> Write the **-spec**'s for your public fun's, and for private fun's when it adds real value for documentation purposes. Define as many types as needed.
263263
264-
```erlang
265-
-type option_id():: atom().
266-
-type option_count():: non_neg_integer().
267-
-type option_percnt():: non_neg_integer().
268-
-type option():: {option_id(), option_count()}.
269-
-type result():: {option_id(), option_percnt()}.
270-
271-
-spec calc([option()]) -> [result()].
272-
calc(Options) ->
273-
TotalCounts = [ X || {_,X} <- Options],
274-
calc(Options, lists:sum(TotalCounts)).er
275-
```
264+
*Examples*: [specs](src/specs.erl)
276265

277-
*Reasoning*: Dialyzer output is complicated as is, and is improved with good type names. In general, having semantically loaded type names for arguments makes reasoning about possible type failures easier, as well as the function's purpose.
266+
*Reasoning*: Dialyzer output is complicated as is, and it is improved with good type names. In general, having semantically loaded type names for arguments makes reasoning about possible type failures easier, as well as the function's purpose.
278267

279268
***
280269
##### Avoid records in specs

src/specs.erl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-module(specs).
2+
3+
-export([bad/2, good/2]).
4+
5+
bad(InitialValue, Commands) ->
6+
gen_server:call(?MODULE, {compute, InitialValue, Commands}).
7+
8+
-type command() :: inc | dec.
9+
-spec good(pos_integer(), [command()]) -> pos_integer().
10+
good(InitialValue, Commands) ->
11+
gen_server:call(?MODULE, {compute, InitialValue, Commands}).

0 commit comments

Comments
 (0)