Skip to content

Commit 70fb047

Browse files
committed
Types in records
1 parent 1fdd1ac commit 70fb047

6 files changed

+31
-7
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Table of Contents:
4343
* [Records go first](#records-go-first)
4444
* [Don't share your records](#dont-share-your-records)
4545
* [Avoid records in specs](#avoid-records-in-specs)
46+
* [Types in records](#types-in-records)
4647
* [Misc](#misc)
4748
* [Write function specs](#write-function-specs)
4849
* [Use -callback attributes over behaviour_info/1](use--callback-attributes-over-behaviour_info1)
@@ -333,6 +334,14 @@ Erlang syntax is horrible amirite? So you might as well make the best of it, rig
333334

334335
*Reasoning*: Types can be exported, which aids documentation and, using ``opaque`` types it also helps with encapsulation and abstraction.
335336

337+
***
338+
##### Types in records
339+
> Always add type definitions to your record fields
340+
341+
*Examples*: [record_types](src/record_types.erl)
342+
343+
*Reasoning*: Records define data structures, and one of the most important parts of that definition is the type of the constituent pieces.
344+
336345
### Misc
337346

338347
***

src/col_width.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-module(col_width).
22

3-
-record(rec, {field1, field2, field3}).
3+
-record(rec, {field1 :: any(), field2 :: any(), field3 :: any()}).
44

55
-export([bad/2, good/2]).
66

src/record_names.erl

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
-export([records/0]).
44

55
-record(badName, {}).
6-
-record(bad_field_name, {badFieldName}).
7-
-record('UPPERCASE', {'THIS_IS_BAD'}).
6+
-record(bad_field_name, {badFieldName :: any()}).
7+
-record('UPPERCASE', {'THIS_IS_BAD' :: any()}).
88

9-
-record(good_name, {good_field_name}).
9+
-record(good_name, {good_field_name :: any()}).
1010

1111
records() -> [#badName{}, #bad_field_name{}, #'UPPERCASE'{}, #good_name{}].

src/record_placement.erl

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
-export([good/0, bad/0]).
44

5-
-record(good, {this, record, appears, before, the_functions}).
5+
-record(good, { this_record :: any()
6+
, appears :: any()
7+
, before :: any()
8+
, the_functions :: any()}).
69

710
good() -> [#good{}].
811

9-
-record(bad, {this, record, appears, below, a_function}).
12+
-record(bad, { this_record :: any()
13+
, appears :: any()
14+
, below :: any()
15+
, a_function :: any()}).
1016

1117
bad() -> [#bad{}].

src/record_spec.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-module(record_spec).
22

3-
-record(state, {field1, field2}).
3+
-record(state, {field1:: any(), field2:: any()}).
44

55
-opaque state() :: #state{}.
66

src/record_types.erl

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-module(record_types).
2+
3+
-export([records/0]).
4+
5+
-record(bad, {no_type}).
6+
7+
-record(good, {with_type :: string()}).
8+
9+
records() -> [#bad{}, #good{}].

0 commit comments

Comments
 (0)