我正在尝试创建一个代表固定长度的二进制包的类型语言。因此,使用固定N字节的位串(例如25字节)似乎是正确的想法。
Elixir声明如下:
## Bitstrings
| <<>> # empty bitstring
| <<_::size>> # size is 0 or a positive integer
| <<_::_*unit>> # unit is an integer from 1 to 256
| <<_::size, _::_*unit>>
由此,我假设您可以使用@spec my_type ::<_::25,_::_*8>>
@type my_type :: <<_::25, _::_*8>>
@spec my_type_test() :: my_type
def my_type_test() do
# 25 byte bitstring with start-of-frame byte
<< 0xA5::size(8) , 0::size(24)-unit(8) >>
end
但是Dialyzer带着下面的话回来了:
[ElixirLS Dialyzer] Invalid type specification for function
'TestModule':my_type_test/0. The success typing
is () -> <<_:200>>
哈?但是它们都是位字符串,位长是一样的!
有人知道为什么Dialyzer不喜欢这个吗?
发布于 2018-06-06 20:49:34
::
后面的数字指定位数,而不是字节数。如果希望类型匹配25字节加上N*8字节,类型需要如下:
@type my_type :: <<_::200, _::_*64>>
在进行此更改后,您的原始表达式将通过Dialyzer的检查,并按预期将大小增加1位或1字节失败。
https://stackoverflow.com/questions/50729162
复制相似问题