Log In  


This code works as expected:

debug=false
(debug and printh or print)("hello")
print("success")

But this gives a runtime error “attempt to call a nil value" at line 4:

debug=false
(debug and printh or print)("hello")
print("success")
(debug and printh or print)("hello again")

Confirmed in both 0.1.8 and 0.1.9.



Oddly, however, this is fine.

debug=false
cprint = debug and printh or print
cprint("hello")
print("success")
cprint("hello again")

debug=false
print("fail?")
(debug and printh or print)("hello")

fails at line 3...


It's actually parsing fine, for what you gave it, and is also not a PICO-8 bug.

[gamax92@test-pc ~]$ lua test.lua
hello
success
lua: test.lua:3: attempt to call a nil value
stack traceback:
test.lua:3: in main chunk
[C]: in ?

The issue here, is that the syntax is ambiguous.
It ends up parsing more like this:

local debug=false
(debug and printh or print)("hello")
print("success")(debug and printh or print)("hello again")

What ends up happening is that it thinks the parenthesis are a function call, and tries to use whatever print returns as the function, which print doesn't return anything, hence the "attempt to call a nil value"
Bug? No. Completely valid Lua, just not what you want. You have to use the semicolon to separate the two.

See this part of the Lua manual: 3.3.1 – Blocks


Ah, makes sense. I vaguely remembered that grammar gotcha but (mistakenly) assumed a newline was enough to disambiguate. Thanks for the clarification.



[Please log in to post a comment]