下面是一个定义不透明类型的模块:
-module(a).
-export([people/0, get_names/1]).
-export_type([person/0]).
-opaque person() :: {person, first_name()}.
-type first_name() :: string().
-spec people() -> People when
People :: [person()].
people() ->
[{person, "Joe"}, {person, "Cindy"}].
-spec get_names(People) -> Names when
People :: [person()],
Names :: [string()].
get_names(Ps) ->
get_names(Ps, []).
get_names([ {person, Name} | Ps ], Names) ->
get_names(Ps, [Name|Names]);
get_names([], Names) ->
lists:reverse(Names).
这是一个在 person(( 类型中乱七八糟的模块:
-module(b).
-export([do/0]).
-spec do() -> Name when
Name :: string().
do() ->
[{person, Name} | _People] = a:people(),
Name.
和透析器:
~/erlang_programs$ dialyzer b.erl
Checking whether the PLT /Users/7stud/.dialyzer_plt is up-to-date... yes
Proceeding with analysis...
Unknown functions:
a:people/0
done in 0m0.49s
done (passed successfully)
我以为透析器会抱怨。
这有效:
$ dialyzer a.erl b.erl
Checking whether the PLT /Users/7stud/.dialyzer_plt is up-to-date... yes
Proceeding with analysis...
b.erl:7: Function do/0 has no local return
b.erl:8: The attempt to match a term of type a:person() against the pattern
{'person', Name} breaks the opaqueness of the term
done in 0m0.52s
done (warnings were emitted)