-opaque
和-type
之间有什么区别?我在erlang核心模块中看到了两者,但感觉不到区别。
可以对它们使用-export_type
吗?
答案 0 :(得分:8)
%% 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.
是的,您可以导出两者,如果不导出它们,则没有区别。