简体   繁体   English

类型列表typespec永不违约

[英]Typed list typespec never breaks the contract

If you define a typespec and use a different type of parameter it will display an error similar to: 如果定义类型规范并使用其他类型的参数,则将显示类似于以下内容的错误:

binary() ... breaks the contract ... boolean()

For example this typespec: 例如,此typespec:

@spec check?(binary) :: boolean

But it doesnt seem to work for a typed List, or at least, it will never display the warning, if I have a method which receives a list of strings I would define this typespec: 但是它似乎不适用于类型列表,或者至少不会显示警告,如果我有一个接收字符串列表的方法,我将定义此类型规范:

@spec check?([String.t]) :: boolean

I can then define any spec for the list and it will never complain when running a dialyzer, ie: 然后,我可以为列表定义任何规范,并且在运行透析器时也不会抱怨,即:

@spec check?(list(boolean)) :: boolean
@spec check?(list(Conn)) :: boolean
@spec check?(list(number)) :: boolean
@spec check?(list(integer)) :: boolean

Is that intended? 那是故意的吗? it looks like if I defined a list with any type [any()] 好像我定义了任何类型的列表[any()]

Is there other way to achieve this? 还有其他方法可以做到这一点吗?

The reason this happens is that all the list types include the empty list as a valid value. 发生这种情况的原因是所有列表类型都将空列表包含为有效值。

For example, in the following case: 例如,在以下情况下:

  • you call the function with a possibly empty list of booleans 您用可能为空的布尔值列表调用该函数
  • the function accepts a possibly empty list of strings 该函数接受一个可能为空的字符串列表

Dialyzer will conclude that there is a possible solution, namely if the list is empty. Dialyzer将得出一个可能的解决方案,即如果列表为空。 Since Dialyzer only prints warnings if it can conclude that a certain piece of code will always crash, it doesn't print one in this case. 由于Dialyzer仅在可以断定某段代码将始终崩溃的情况下才打印警告,因此在这种情况下不打印任何代码。

I'm not aware of any good solution to this. 我不知道有什么好的解决方案。 If you want to explicitly require non-empty lists, you can use eg nonempty_list(boolean) instead of list(boolean) . 如果要明确要求非空列表,则可以使用nonempty_list(boolean)代替list(boolean)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM