From 63d2a28c4e6a868da0f7a4117c615fe05593990d Mon Sep 17 00:00:00 2001
From: Kostis Sagonas
- Erlang is a dynamically typed language. Still, it comes with a
- language extension for declaring sets of Erlang terms to form a
- particular type, effectively forming a specific sub-type of the set
- of all Erlang terms.
-
- Subsequently, these types can be used to specify types of record fields
- and the argument and return types of functions.
-
- Type information can be used to document function interfaces,
- provide more information for bug detection tools such as
+ Erlang is a dynamically typed language. Still, it comes with a
+ notation for declaring sets of Erlang terms to form a particular
+ type, effectively forming a specific sub-type of the set of all
+ Erlang terms.
+
+ Subsequently, these types can be used to specify types of record fields
+ and the argument and return types of functions.
+
+ Type information can be used to document function interfaces,
+ provide more information for bug detection tools such as
- Types describe sets of Erlang terms.
- Types consist and are built from a set of predefined types (e.g.
- For integers and atoms, we allow for singleton types (e.g. the integers
+ For integers and atoms, we allow for singleton types (e.g. the integers
+
- describes the same set of terms as the type union:
-
- Because of sub-type relations that exist between types, types form a lattice
- where the topmost element, any(), denotes the set of all Erlang terms and
- the bottom-most element, none(), denotes the empty set of terms.
-
- The set of predefined types and the syntax for types is given below:
-
+ describes the same set of terms as the type union:
+
+ Because of sub-type relations that exist between types, types
+ form a lattice where the topmost element,
+ The set of predefined types and the syntax for types is given below:
+
+ The general form of bitstrings is
Because lists are commonly used, they have shorthand type notations.
The type
+ In addition, the following three built-in types exist and can be
+ thought as defined below, though strictly their "type definition" is
+ not valid syntax according to the type language defined above.
+
Users are not allowed to define types with the same names as the
predefined or built-in ones. This is checked by the compiler and
its violation results in a compilation error.
- (For bootstrapping purposes, it can also result to just a warning if this
- involves a built-in type which has just been introduced.)
+ (For bootstrapping purposes, it can also result to just a warning
+ if this involves a built-in type which has just been introduced.)
where the following two types
define the set of Erlang terms one would expect:
Also for convenience, we allow for record notation to be used.
Records are just shorthands for the corresponding tuples.
Records have been extended to possibly contain type information.
This is described in the sub-section
where the type name is an atom (
A module can export some types in order to declare that other modules
are allowed to refer to them as remote types.
This declaration has the following form:
- atom() | 'bar' | integer() | 42
-
-atom() | integer()
- atom() | 'bar' | integer() | 42
+ atom() | integer()
+ >
+ | <<_:M>> %% M is a positive integer
+ | <<_:_*N>> %% N is a positive integer
+ | <<_:M, _:_*N>>
-Binary :: binary() %% <<_:_ * 8>>
- | <<>>
- | <<_:Erlang_Integer>> %% Base size
- | <<_:_*Erlang_Integer>> %% Unit size
- | <<_:Erlang_Integer, _:_*Erlang_Integer>>
+ Fun :: fun() %% any function
+ | fun((...) -> Type) %% any arity, returning Type
+ | fun(() -> Type)
+ | fun((TList) -> Type)
-Fun :: fun() %% any function
- | fun((...) -> Type) %% any arity, returning Type
- | fun(() -> Type)
- | fun((TList) -> Type)
+ Integer :: integer()
+ | Erlang_Integer %% ..., -1, 0, 1, ... 42 ...
+ | Erlang_Integer..Erlang_Integer %% specifies an integer range
-Integer :: integer()
- | Erlang_Integer %% ..., -1, 0, 1, ... 42 ...
- | Erlang_Integer..Erlang_Integer %% specifies an integer range
+ List :: list(Type) %% Proper list ([]-terminated)
+ | improper_list(Type1, Type2) %% Type1=contents, Type2=termination
+ | maybe_improper_list(Type1, Type2) %% Type1 and Type2 as above
-List :: list(Type) %% Proper list ([]-terminated)
- | improper_list(Type1, Type2) %% Type1=contents, Type2=termination
- | maybe_improper_list(Type1, Type2) %% Type1 and Type2 as above
+ Tuple :: tuple() %% stands for a tuple of any size
+ | {}
+ | {TList}
-Tuple :: tuple() %% stands for a tuple of any size
- | {}
- | {TList}
+ TList :: Type
+ | Type, TList
-TList :: Type
- | Type, TList
+ Union :: Type1 | Type2
]]>
+
+
+
-nonempty_maybe_improper_list(Type) :: nonempty_maybe_improper_list(Type, any())
-nonempty_maybe_improper_list() :: nonempty_maybe_improper_list(any())
+ nonempty_maybe_improper_list(Type) :: nonempty_maybe_improper_list(Type, any())
+ nonempty_maybe_improper_list() :: nonempty_maybe_improper_list(any())
-nonempty_improper_list(Type1, Type2)
-nonempty_maybe_improper_list(Type1, Type2)
+ nonempty_improper_list(Type1, Type2)
+ nonempty_maybe_improper_list(Type1, Type2)
-Record :: #Erlang_Atom{}
- | #Erlang_Atom{Fields}
+ Record :: #Erlang_Atom{}
+ | #Erlang_Atom{Fields}
--type my_struct_type() :: Type.
--opaque my_opaq_type() :: Type.
+ -type my_struct_type() :: Type.
+ -opaque my_opaq_type() :: Type.
--type orddict(Key, Val) :: [{Key, Val}].
+ -type orddict(Key, Val) :: [{Key, Val}].
--export_type([T1/A1, ..., Tk/Ak]).
+ -export_type([T1/A1, ..., Tk/Ak]).
where the Ti's are atoms (the name of the type) and the Ai's are their
arguments. An example is given below:
--export_type([my_struct_type/0, orddict/2]).
+ -export_type([my_struct_type/0, orddict/2]).
Assuming that these types are exported from module
-mod:my_struct_type()
-mod:orddict(atom(), term())
+ mod:my_struct_type()
+ mod:orddict(atom(), term())
One is not allowed to refer to types which are not declared as exported.
@@ -317,19 +343,19 @@ mod:orddict(atom(), term()) record. The syntax for this is:
--record(rec, {field1 :: Type1, field2, field3 :: Type3}).+ -record(rec, {field1 :: Type1, field2, field3 :: Type3}).
For fields without type annotations, their type defaults to any(). I.e., the above is a shorthand for:
--record(rec, {field1 :: Type1, field2 :: any(), field3 :: Type3}).+ -record(rec, {field1 :: Type1, field2 :: any(), field3 :: Type3}).
In the presence of initial values for fields, the type must be declared after the initialization as in the following:
--record(rec, {field1 = [] :: Type1, field2, field3 = 42 :: Type3}).+ -record(rec, {field1 = [] :: Type1, field2, field3 = 42 :: Type3}).
Naturally, the initial values for fields should be compatible with (i.e. a member of) the corresponding types. @@ -340,13 +366,13 @@ mod:orddict(atom(), term()) effects:
--record(rec, {f1 = 42 :: integer(), - f2 :: float(), - f3 :: 'a' | 'b'}). + -record(rec, {f1 = 42 :: integer(), + f2 :: float(), + f3 :: 'a' | 'b'}). --record(rec, {f1 = 42 :: integer(), - f2 :: 'undefined' | float(), - f3 :: 'undefined' | 'a' | 'b'}).+ -record(rec, {f1 = 42 :: integer(), + f2 :: 'undefined' | float(), + f3 :: 'undefined' | 'a' | 'b'}).
For this reason, it is recommended that records contain initializers, whenever possible. @@ -355,15 +381,13 @@ mod:orddict(atom(), term()) Any record, containing type information or not, once defined, can be used as a type using the syntax:
--#rec{}+
#rec{}
In addition, the record fields can be further specified when using a record type by adding type information about the field in the following manner:
--#rec{some_field :: Type}+
#rec{some_field :: Type}
Any unspecified fields are assumed to have the type in the original
record declaration.
@@ -377,7 +401,7 @@ mod:orddict(atom(), term())
compiler attribute
--spec Module:Function(ArgType1, ..., ArgTypeN) -> ReturnType.+ -spec Module:Function(ArgType1, ..., ArgTypeN) -> ReturnType.
The arity of the function has to match the number of arguments, or else a compilation error occurs. @@ -392,19 +416,19 @@ mod:orddict(atom(), term()) For most uses within a given module, the following shorthand suffices:
--spec Function(ArgType1, ..., ArgTypeN) -> ReturnType.+ -spec Function(ArgType1, ..., ArgTypeN) -> ReturnType.
Also, for documentation purposes, argument names can be given:
--spec Function(ArgName1 :: Type1, ..., ArgNameN :: TypeN) -> RT.+ -spec Function(ArgName1 :: Type1, ..., ArgNameN :: TypeN) -> RT.
A function specification can be overloaded.
That is, it can have several types, separated by a semicolon (
--spec foo(T1, T2) -> T3 - ; (T4, T5) -> T6.+ -spec foo(T1, T2) -> T3 + ; (T4, T5) -> T6.
A current restriction, which currently results in a warning (OBS: not an error) by the compiler, is that the domains of @@ -412,8 +436,8 @@ mod:orddict(atom(), term()) For example, the following specification results in a warning:
--spec foo(pos_integer()) -> pos_integer() - ; (integer()) -> integer().+ -spec foo(pos_integer()) -> pos_integer() + ; (integer()) -> integer().
Type variables can be used in specifications to specify relations for the input and output arguments of a function. @@ -421,47 +445,66 @@ mod:orddict(atom(), term()) polymorphic identity function:
--spec id(X) -> X.+ -spec id(X) -> X.
However, note that the above specification does not restrict the input and output type in any way. - We can constrain these types by guard-like subtype constraints: + We can constrain these types by guard-like subtype constraints + and provide bounded quantification:
---spec id(X) -> X when is_subtype(X, tuple()).+
-spec id(X) -> X when X :: tuple().
- or equivalently by the more succinct and more modern form of the above: -
---spec id(X) -> X when X :: tuple().-
- and provide bounded quantification. Currently, the
+ The above function specification, using multiple occurrences of + the same type variable, provides more type information than the + function specification below where the type variables are missing: +
+-spec id(tuple()) -> tuple().+
+ The latter specification says that the function takes some tuple
+ and returns some tuple, while the one with the
+ However, it's up to the tools that process the specs to choose + whether to take this extra information into account or ignore it. +
+
The scope of an
--spec foo({X, integer()}) -> X when X :: atom() - ; ([Y]) -> Y when Y :: number().+ -spec foo({X, integer()}) -> X when X :: atom() + ; ([Y]) -> Y when Y :: number(). +
-spec id(X) -> X when is_subtype(X, tuple()).+
+ but its use is discouraged. It will be taken out in a future + Erlang/OTP release. +
+Some functions in Erlang are not meant to return; either because they define servers or because they are used to throw exceptions as the function below:
--my_error(Err) -> erlang:throw({error, Err}).+
my_error(Err) -> erlang:throw({error, Err}).
- For such functions we recommend the use of the special no_return()
+ For such functions we recommend the use of the special
--spec my_error(term()) -> no_return().+
-spec my_error(term()) -> no_return().-- cgit v1.2.3