Unnamed anonymous functions give "Ambiguous syntax" errors if previous line contains a function call

I’d like to call unnamed anonymous function calls but for some reason whenever I have any type of function call on the lines above the function, I get this error:

Ambiguous syntax: this looks like an argument list for a function call, but could also be a start of new statement

Here’s a working code example:

(function()
	print("Printing in an unnamed anonymous function!")
end)()

What gives me the error?

print("Hello")

(function()
	print("Printing in an unnamed anonymous function!")
end)()

This only occurs with function calls on the lines above. I’m assuming this is because Lua doesn’t have any sort of code style guide, meaning it probably reads the code as a single line snippet which is why it breaks but I’m not sure.

I find this really annoying because this is valid legal Lua syntax.

Do you guys have any idea?

2 Likes

Yeah well this is one of the few cases that using semicolons actually fix the problem here.

The problem here is that intepreter intepretes this code like this:

print("Hello")(function()print("Printing in an unnamed anonymous function!")end)()
--Basically Lua tries to call the result returned by the print function, which is nil so that throws an error.

While you definetly wouldn’t want to code likes this, you can just add a semicolon at the end of the print line to show that nil isn’t being called with that anonymous fuction passed to it as the first argument:

print("Hello");
(function()
	print("Printing in an unnamed anonymous function!")
end)()
2 Likes

Thanks for providing a solution and an explanation, I thought as much that the problem lies with the interpreter, just very annoying. Quite an unusual situation, thank you once again.