gpt4 book ai didi

Erlang Dialyzer 整数范围

转载 作者:行者123 更新时间:2023-12-04 01:46:23 25 4
gpt4 key购买 nike

-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 .

运行 dialyzer(和 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 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

这是“预期的”行为吗?

最佳答案

是的,这似乎是“预期的”。查看源代码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 上有传递引用网站。

A range of integers. For example, if you wanted to represent a number of months in a year, the range 1..12 could be defined. Note that Dialyzer reserves the right to expand this range into a bigger one.

但是在 google 的首页上没有官方使用关键字 dialyzer integer ranges

编辑...如果您尝试仔细观察,您会发现:

-module(test).
-export([h/0]).

-spec h() -> RESULT when
RESULT :: 1..13 .

h () -> 100 .

Dialyzer 会捕获错误! (打字机不会)……

关于Erlang Dialyzer 整数范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43307949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com