这是一个定义opaque类型的模块:
-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).
这是一个模拟人()类型内的模块:
-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)
我期待透析器抱怨。
答案 0 :(得分:1)
这有效:
$ 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)