我正在学习 Erlang 并尝试使用 Dialyzer 在可能的情况下获得最大的类型安全性。有一点我不明白:什么是非终止函数的类型以及如何在 -spec
中表示它.任何人都可以对此有所了解吗?
最佳答案
一个永远循环且永不终止的函数具有返回类型 no_return()
. (该返回类型也用于总是抛出异常的函数,例如自定义错误函数。如果您没有指定该返回类型,Dialyzer 会告诉您该函数“没有本地返回”。)
这在 Types and Function Specifications 中提到Erlang引用手册的章节:
Some functions in Erlang are not meant to return; either because they define servers or because they are used to throw exceptions, as in the following function:
my_error(Err) -> erlang:throw({error, Err}).
For such functions, it is recommended to use the special
no_return()
type for their "return", through a contract of the following form:-spec my_error(term()) -> no_return().
关于types - Erlang 中非终止函数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37184719/