-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 .
运行透析器(和typer)只捕获函数f
。
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相同
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
这是"预期"行为?
答案 0 :(得分:2)
是的,似乎确实是"预期"。 查看源代码here 它测试
的值 -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;
恕我直言,这似乎很危险,并没有提供任何真正的保证。它应该清楚地记录下来。 learn you some Erlang网站上有传递参考。
一系列整数。例如,如果您想表示一个数字 一年中的几个月,可以定义1..12的范围。注意 Dialyzer保留扩大此范围的权利。
但谷歌首页使用关键字dialyzer integer ranges
编辑...看得更近,你可以看到,如果你尝试:
-module(test).
-export([h/0]).
-spec h() -> RESULT when
RESULT :: 1..13 .
h () -> 100 .
透析师会发现错误! (Typer不会)......
答案 1 :(得分:2)
是的,预期&#34;行为。或者更确切地说&#34;接受&#34;。
免责声明:
<强>解释强>
Dialyzer的设计师已经决定使用这样的过度使用(以及其他原因)在分析递归函数时使工具的类型推断分析终止(达到修复点)(内部步骤看起来真的像这样:&#34; factorial的基本情况适用于0,因此它的递归情况也适用于1,所以它也适用于2,所以它也适用于3,[...],所以它适用于12,确定所以它也适用于任何char()
,但它也适用于char_range + 1
,因此它适用于所有integers()
&#34;)。
这种(任意确实)限制变得至关重要,而Dialyzer再也没有承诺报告任何事情......