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.

P#28540 2016-09-15 03:57 ( Edited 2016-09-15 07:57)

Oddly, however, this is fine.

debug=false
cprint = debug and printh or print
cprint("hello")
print("success")
cprint("hello again")
P#28543 2016-09-15 10:52 ( Edited 2016-09-15 14:52)
debug=false
print("fail?")
(debug and printh or print)("hello")

fails at line 3...

P#28551 2016-09-15 14:19 ( Edited 2016-09-15 18:21)

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

P#28593 2016-09-16 04:35 ( Edited 2016-09-16 08:46)

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

P#28594 2016-09-16 04:51 ( Edited 2016-09-16 08:51)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2022-12-03 15:17:22 | 0.026s | Q:25