Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type vs opaque directives in erlang

Tags:

erlang

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?

like image 780
egor7 Avatar asked Feb 22 '12 15:02

egor7


People also ask

What is type in Erlang?

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.

What are the two types of number formats supported by Erlang?

In Erlang there are 2 types of numeric literals which are integers and floats.

What is spec in Erlang?

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.

How do I use Erlang dialyzer?

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.


1 Answers

%% 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.

like image 151
Alexey Romanov Avatar answered Nov 12 '22 07:11

Alexey Romanov