gpt4 book ai didi

exception - Erlang 中的 function_clause 和 badarg 有什么区别?

转载 作者:行者123 更新时间:2023-12-02 00:51:30 25 4
gpt4 key购买 nike

有时我得到的是 badarg 而不是 function_clause 但我看不到确定将要出现哪一个的规则。

据我所知,function_clause 在没有函数的实现与给定参数匹配时抛出。关于 badarg 文档说

The argument is of wrong data type, or is otherwise badly formed.

这似乎被 function_clause 条件覆盖了......

例如

lists:flatten(3).

抛出子句错误,而类型肯定不匹配。

最佳答案

函数子句是具有不同参数模式/守卫的定义的一部分,如

func({X, Y}) -> ...;
func(X) when X > 10 -> ...;
func(_) -> ...

function_clause 表示它们都不匹配。还有类似的if_clausecase_clause。对于具有单个参数的 flatten,有 only one clause

flatten(List) when is_list(List) ->
do_flatten(List, []).

它与 3 不匹配,这就是您获得 function_clause 的原因。

所以

Sometimes I get badarg instead of function_clause but I cannot see the rule that would determine which one is going to appear

这基本上是一个实现细节,只有在调试该函数的实现时才应该关心。

关于exception - Erlang 中的 function_clause 和 badarg 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57235420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com