6

Ran into this problem earlier. For a multi return value function

fn=function() return 'a','b' end

the call

print(fn()) returns a b

but the call

print(fn() or nil) returns only a

why? or should not matter since the first call was successful right?

1 Answer 1

6

Quoting from Programming in Lua §5.1 – Multiple Results

Lua always adjusts the number of results from a function to the circumstances of the call. When we call a function as a statement, Lua discards all results from the function. When we use a call as an expression, Lua keeps only the first result. We get all results only when the call is the last (or the only) expression in a list of expressions.

In the case of your example, the return value of fn() is used as an expression (the left operand of or operator), so only the first value is kept.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.