本文介绍了Erlang Dialyzer整数范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
-module(test).
-export([f/0, g/0]).
-spec f() -> RESULT when
RESULT :: 0..12 .
-spec g() -> RESULT when
RESULT :: 0..13 .
f () -> 100 .
g () -> 100 .
仅运行功能f
的透析器(和键入器)正在运行.
Running dialyzer (and typer) only the function f
is caught.
dialyzer test.erl
Checking whether the PLT /Users/ben/.dialyzer_plt is up-to-date... yes
Proceeding with analysis...
test.erl:4: Invalid type specification for function test:f/0. The success typing is () -> 100
done in 0m0.53s
done (warnings were emitted)
与打字机相同
typer test.erl
typer: Error in contract of function test:f/0
The contract is: () -> RESULT when RESULT :: 0..12
but the inferred signature is: () -> 100
这是预期"行为吗?
推荐答案
是的,它似乎确实是预期的".查看源代码此处它针对
Yes it does seem to be "expected".Looking at the source code hereIt tests against the value of
-define(SET_LIMIT, 13).
在测试中
t_from_range(X, Y) when is_integer(X), is_integer(Y) ->
case ((Y - X) < ?SET_LIMIT) of
true -> t_integers(lists:seq(X, Y));
false ->
case X >= 0 of
false ->
if Y < 0 -> ?integer_neg;
true -> t_integer()
end;
true ->
if Y =< ?MAX_BYTE, X >= 1 -> ?int_range(1, ?MAX_BYTE);
Y =< ?MAX_BYTE -> t_byte();
Y =< ?MAX_CHAR, X >= 1 -> ?int_range(1, ?MAX_CHAR);
Y =< ?MAX_CHAR -> t_char();
X >= 1 -> ?integer_pos;
X >= 0 -> ?integer_non_neg
end
end
end;
恕我直言,这似乎很危险,并且没有提供任何真正的保证.绝对应该清楚地记录下来.在学习一些Erlang网站上.
IMHO this seems dangerous, and does not provide any real guarantees. It should definitely be documented clearly. there is passing reference on the learn you some Erlang website.
但是使用关键字dialyzer integer ranges
编辑...再仔细一点,您会发现,如果尝试:
Edit... looking a bit closer you can see that if you try:
-module(test).
-export([h/0]).
-spec h() -> RESULT when
RESULT :: 1..13 .
h () -> 100 .
Dialyzer将捕获该错误! (Typer不会)...
Dialyzer will catch the error! (Typer will not) ...
这篇关于Erlang Dialyzer整数范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!