Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can pcall return value of called function instead of boolean result true/false?

Tags:

lua

Can pcall return value of called function instead of boolean result true/false?

for example

function f()
return 'some text'
end

print(tostring(pcall(f)))

print will show only true or false instead of value returned by f

like image 871
user3309760 Avatar asked Dec 15 '14 13:12

user3309760


2 Answers

tostring selects only first parameter.

a,b = pcall(f)
print(b) --> 'some text'
like image 82
das Avatar answered Dec 25 '22 11:12

das


function f()
  return 'some text'
end

local status, res = pcall(f)

if pcall() succeeded, status is true and res is the return value of f(). if pcall() failed, status is false and res is the error message.

like image 41
Brad Pitt Avatar answered Dec 25 '22 13:12

Brad Pitt