Lukas Knudsen
Lukas Knudsen

Reputation: 339

Lua - Error in execution of return and or

I am basically doing some testing to get to know the Lua language a bit better. I found a bug that makes zero sense to me.

Function:

local function d(c)
    return (!c and print("c", false) or print("c", true))
end

local function a(b, c)
    return (!b and d(c) or print("b", true))
end

When i run a(1, nil) or a(1, 1) it outputs b true, but if i run a(nil, 1) then it outputs c true and b true

If anyone could enlighten me on why it returns two value when that technically shouldn't be possible?

Upvotes: 1

Views: 371

Answers (1)

cyclaminist
cyclaminist

Reputation: 1807

Maybe you already understand what's happening, but I've already written this post. Lua doesn't have a ! operator; I guess you meant not. (I wouldn't be surprised if someone has made a patched version of Lua with ! in place of not.)

a(nil, 1) returns not nil and d(1) or print("b", true). Now, not nil evaluates to true and d(1) evaluates to nil, so we have true and nil or print("b", true), which in turn evaluates to nil or print("b", true), and therefore print("b", true) is evaluated.

As to why d(1) evaluates to nil: it returns not 1 and print("c", false) or print("c", true). That is equivalent to not 1 and nil or nil because print always returns nothing when it is called, and nothing is treated as nil by the operators and and or. not x and nil or nil always evaluates to nil whether x is truthy or falsy, so d always returns nil. (The only difference is that if d receives a falsy value, both print calls are evaluated.)

You can verify that print returns nothing by calling type(print('a')): it throws the error "bad argument #1 to 'type' (value expected)", whereas type(nil) returns "nil".

Upvotes: 2

Related Questions