gpt4 book ai didi

exception - 为什么我的变量在 erlang try 中不安全?

转载 作者:行者123 更新时间:2023-12-02 04:01:35 25 4
gpt4 key购买 nike

我是 Erlang 新手。我对下面的代码有问题:

try
{ok, [Card]} = io:fread("Input card number: ","~d")
catch
error:K -> (Card=0);
exit:K -> (Card=0);
throw:K -> (Card=0)
end,

我不知道为什么'变量'Card'在'try'中不安全。对此我能做什么?

最佳答案

像您这样的代码可以在 case 表达式中工作,但对于 try 来说,这是 Erlang 中变量绑定(bind)规则的异常(exception)。来自 the reference manual :

For the try expression introduced in Erlang 5.4/OTP R10B, variable scoping is limited so that variables bound in the expression are always 'unsafe' outside the expression. This is to be improved.

您可以这样编写代码:

Card =
try
{ok, [C]} = io:fread("Input card number: ","~d"),
C
catch
error:K -> 0;
exit:K -> 0;
throw:K -> 0
end,

关于exception - 为什么我的变量在 erlang try 中不安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41640080/

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