Erlang 无法获取不透明类型以导致透析器中的抽象冲突
以下是定义不透明类型的模块:Erlang 无法获取不透明类型以导致透析器中的抽象冲突,erlang,dialyzer,Erlang,Dialyzer,以下是定义不透明类型的模块: -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() ->
-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)
我以为透析器会抱怨。这是有效的:
$ 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)
透析器使用.plt类型的查找表。警告未知功能:a:people/0表示,在检查b时透析器不知道模块a。您通过指定必须在CLI上检查的所有模块来解决此问题。您还可以从模块中构建.plt。基本上,您将构建一个initial.plt,然后向其中添加一个。那么你可以在检查b时使用这个.plt。