pcall可以返回被调用函数的值而不是布尔结果的真/假吗?

use*_*760 11 lua

pcall可以返回被调用函数的值而不是布尔结果的真/假吗?

例如

function f()
return 'some text'
end

print(tostring(pcall(f)))
Run Code Online (Sandbox Code Playgroud)

print将仅显示true或false,而不是f返回的值

das*_*das 15

tostring只选择第一个参数.

a,b = pcall(f)
print(b) --> 'some text'
Run Code Online (Sandbox Code Playgroud)


Bra*_*itt 5

function f()
  return 'some text'
end

local status, res = pcall(f)
Run Code Online (Sandbox Code Playgroud)

如果pcall ()成功,则status为true,resf ()的返回值。如果pcall () 失败,则status为 false,res为错误信息。