Using Lua reserved words in function names

Hey there, pretty simple question.

Is it possible to name a function a Lua reserved word, such as print, or next?

I tried using quotation marks and apostrophes (like 'next' or "next"), however it still returned an error. No big deal if it isn’t possible, but it’s more for organization’s sake.

print and next are names, not reserved keywords. in, and, and others are reserved.

function print() end -- works
function and() end -- does not

All reserved keywords are under this section: Lua 5.3 Reference Manual

5 Likes

No, you cannot.
Also print and next are just names of builtin functions, they’re not reserved keywords.

1 Like

Still underlines in orange, any way to get rid of it, thinking that’s what mislead me. No big deal if it isn’t possible. Also didn’t know that was possible, thanks!

The orange underline is probably just a warning if you are using a function name that is the same as a predefined global or such. You can hover over it to see what it’s complaining about.

2 Likes

It indicates that it’ll overridden. For some reason it’s not underlined when doing local function print() instead of function print(), may that’ll work?

1 Like

Yeah, this is the warning, it says to consider changing it to local. Too bad there isn’t a way to ignore it as you can with MS Word or something.
Screen Shot 2020-06-11 at 8.24.58 PM

You can follow the suggestion by the editor and write local function next() instead. In fact, all of your functions should already be declared local anyways.

1 Like

Sometimes it can cause an issue like this (assuming I wanted recursion to happen):

Screen Shot 2020-06-11 at 8.58.53 PM

You can forward declare locals, too.

local number2

local function number1() number2() end

function number2() end -- omit `local`, because it was forward declared
2 Likes

Wait, so you can use that local “variable” as a function? This is news to me.

Doing local x will just localize x to that scope, while function x() will set the value of x to that function. If you were to run number1() before defining number2 as a variable, it’d still error since number2 doesn’t have a value yet.

3 Likes

Functions in Lua are stored as variables anyway, meaning you can do stuff like this:

local oldPrint = print

print = function(...)
	oldPrint("Overwritten print function!")
	warn(...)
end

print("hey")
1 Like