在 Erlang 中标记错误有好处吗?

Is there benefit to tagging errors in Erlang?

在 Erlang 中,标记成功的 return 值是有好处的(例如,如 Erlang Programming Rules and Conventions), but is there a benefit to tagging failure values? Specifically, is there benefit to the style of tagging used in the file package, where errors are tagged with the error atom? For instance, file:open returns {error, Reason} 中所示,当发生错误时,其中 Reason 是一个更具描述性的原子,但是当你可以直接 return Reason 原子时,是否需要 error 标签?我目前能看到的唯一优势是标记将 Reason 原子记录为错误,但我觉得错误原子是自我描述的,并且必须 "unbox" 来自此函数的所有未来错误值超过了这个优势。

不仅仅是 file 模块用 error 原子标记错误;相反,这是一种非常常见的 Erlang 做法。好处是任何想要检查错误而不用担心原因的代码都可以匹配 {error, _Reason} 并采取适当的行动,而如果它自己匹配 Reason ——顺便说一句, 并不总是一个原子——这种匹配会根据调用的内容而有很大不同,并且在代码中也更难看到。