What is the difference between -opaque
and -type
? I saw both in erlang core modules but can't feel the difference.
Is it possible to use -export_type
for both of them?
Types describe sets of Erlang terms. Types consist of, and are built from, a set of predefined types, for example, integer(), atom(), and pid(). Predefined types represent a typically infinite set of Erlang terms that belong to this type. For example, the type atom() denotes the set of all Erlang atoms.
In Erlang there are 2 types of numeric literals which are integers and floats.
spec adds up information about the code. It indicates the arity of the function and combined with -type declarations, are helpful for documentation and bug detection tools. Tools like Edoc use these type specifications for building documentation. Tools like Dialyzer use it for static analysis of the code.
When using Dialyzer from the command line, send the analysis results to the specified outfile rather than to stdout. Store the PLT at the specified file after building it. Include dir in the path for Erlang. This is useful when analyzing files that have -include_lib() directives.
%% module1.erl
-export_type([my_tup1/0, my_tup2/0]).
-type my_tup1() :: {any(), any()}.
-opaque my_tup2() :: {any(), any()}.
%% module2.erl
-spec foo1(module1:my_tup1()) -> ok.
foo1({_, _}) -> ok. %% fine
-spec foo2(module1:my_tup2()) -> ok.
foo2({_, _}) -> ok.
%% Dialyzer warning, because you are looking at
%% the internal structure of a my_tup2() term.
%% If you defined the same function in the module module1, it wouldn't give a warning.
foo2(_) -> ok. %% no warning again.
Yes, you can export both, and if you don't export them, there is no difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With