erlang中非终止函数的类型 [英] Type of non-terminating function in erlang

查看:130
本文介绍了erlang中非终止函数的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习erlang,并尝试使用透析器来尽可能获得最大的类型安全性.有一件事我不理解:非终止函数的类型是什么以及如何在规范中表示它.您能说明一下吗?

I'm learning erlang and trying to use dialyzer to get maximum type-safety when it's possible. There's a thing that I don't understand: what is the type of non-terminating function and how to denote it in spec. Could you please shed a light on this?

推荐答案

一个永久循环且永不终止的函数的返回类型为no_return(). (该返回类型还用于总是抛出异常的函数,例如自定义错误函数.如果您未指定该返回类型,则Dialyzer会告诉您该函数没有本地返回".)

A function that loops forever and never terminates has the return type no_return(). (That return type is also used for functions that always throw exceptions, e.g. a custom error function. If you don't specify that return type, Dialyzer will tell you that the function "has no local return".)

《 Erlang参考手册》的类型和功能规范一章对此进行了提及:

This is mentioned in the Types and Function Specifications chapter of the Erlang Reference Manual:

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

对于此类功能,建议通过以下形式的合同将特殊的no_return()类型用于其返回":

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

这篇关于erlang中非终止函数的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆