-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Rule: Consistent Generic Type (#259)
* New Rule: Types term or any * New Rule: Types term or any - additional changes post-feedback * Update src/elvis_style.erl Co-authored-by: Paulo F. Oliveira <[email protected]> * New Rule: Types term or any - additional changes post-feedback v2 * Update doc_rules/elvis_style/consistent_generic_type.md Co-authored-by: Paulo F. Oliveira <[email protected]>
- Loading branch information
1 parent
0ebed61
commit 0216160
Showing
8 changed files
with
221 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Consistent Generic Type | ||
|
||
Use `term()` or `any()` consistently for types in specs. | ||
|
||
> Works on `.beam` file? Yes. | ||
## Options | ||
|
||
- `preferred_type :: term | any`. | ||
- default: `term`. | ||
|
||
## Example | ||
|
||
```erlang | ||
{elvis_style, consistent_generic_type, #{ preferred_type => term }} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-module(consistent_generic_type_any). | ||
|
||
-export([simple_any/1, simple_combined/1, simple_when/1]). | ||
|
||
% Type definitions when this is alone or combined | ||
-type my_type() :: any(). | ||
-type combined() :: any() | my_type(). | ||
|
||
% Record definitions | ||
-record(my_record, {c :: combined(), a :: any()}). | ||
|
||
% Callback definitions (with or without when) | ||
-callback my_callback(any()) -> any(). | ||
-callback my_callback_when(X) -> X when X :: any(). | ||
|
||
|
||
-spec simple_any(any()) -> ok. | ||
simple_any(_Args) -> ok. | ||
|
||
-spec simple_combined(combined()) -> ok. | ||
simple_combined(_Args) -> ok. | ||
|
||
-spec simple_when(#my_record{}) -> {combined(), X} when X :: any(). | ||
simple_when(#my_record{c = C, a = A}) -> {C, A}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-module(consistent_generic_type_no_checks). | ||
|
||
-export([any/0, term/0, my_function/1]). | ||
|
||
% A parametric type called any | ||
-type any(Thing) :: Thing. | ||
|
||
% A parametric type called term | ||
-type term(Thing) :: Thing. | ||
|
||
% Record definitions with attributes named term/any | ||
-record(my_record, {term :: term, any :: any}). | ||
|
||
|
||
% A function called any | ||
-spec any() -> any. | ||
any() -> any. | ||
|
||
% A function called term | ||
-spec term() -> term. | ||
term() -> term. | ||
|
||
|
||
% A function that calls the function called any | ||
-spec my_function(Thing :: any | term) -> any | term. | ||
my_function(#my_record{any = any}) -> any(); | ||
my_function(#my_record{term = term}) -> term(). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-module(consistent_generic_type_term). | ||
|
||
-export([simple_term/1, simple_combined/1, simple_when/1]). | ||
|
||
% Type definitions when this is alone or combined | ||
-type my_type() :: term(). | ||
-type combined() :: term() | my_type(). | ||
|
||
% Record definitions | ||
-record(my_record, {c :: combined(), t :: term()}). | ||
|
||
% Callback definitions (with or without when), | ||
-callback my_callback(term()) -> term(). | ||
-callback my_callback_when(X) -> X when X :: term(). | ||
|
||
|
||
-spec simple_term(term()) -> ok. | ||
simple_term(_Args) -> ok. | ||
|
||
-spec simple_combined(combined()) -> ok. | ||
simple_combined(_Args) -> ok. | ||
|
||
% Specs with when | ||
-spec simple_when(#my_record{}) -> {combined(), X} when X :: term(). | ||
simple_when(#my_record{c = C, t = T}) -> {C, T}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-module(consistent_generic_type_term_and_any). | ||
|
||
-export([simple_term/1, simple_combined/1, simple_when/1]). | ||
|
||
% Type definitions when this is alone or combined | ||
-type my_type() :: term(). | ||
-type combined() :: any() | my_type(). | ||
|
||
% Record definitions | ||
-record(my_record, {t :: term(), a :: any()}). | ||
|
||
% Callback definitions (with or without when) | ||
-callback my_callback(term()) -> any(). | ||
-callback my_callback_when(X) -> X when X :: term(). | ||
|
||
|
||
-spec simple_term(term()) -> ok. | ||
simple_term(_Args) -> ok. | ||
|
||
-spec simple_combined(combined()) -> ok. | ||
simple_combined(_Args) -> ok. | ||
|
||
% Specs with when | ||
-spec simple_when(#my_record{}) -> {any(), X} when X :: term(). | ||
simple_when(#my_record{a = A, t = T}) -> {A, T}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters