Search code examples
lualua-5.2

Is it safe to use "type" as a name/identifier in Lua?


OK, accordingly to PiL 2.1 – Lexical Conventions it should be possible since it's not listed as reserved keyword, but it's indeed used to return elements type like this type("whatever") and VS Code sometimes colorizes it and sometimes not depending on context... Plus, if I do something like this local type = "123" print(type) it seems to print "123" without problem in ZeroBrane Studio, but in the hosting program I script for (both equally running Lua 5.2), for some reason it throws the following error after printing: "attempt to call upvalue 'type' (a string value)".

Well thus my confusion, is it safe to use it since it's not listed as reserved keyword or not? And if not, why is not listed there? I've always tried to avoid it in favor of, e.g., "kind" just in case, but now I want to be sure about what's going on here (if something 🙄) and I hope it makes sense... Thanks.


Solution

  • You only shadow the variable, it's safe. However, I do not recommend to shadow global variables. It's prone to errors (e.g. you forgot to declare a local and suddenly you overwrite type) and you obviously no longer have access to that globals. Some IDEs also highlight inbuilts, which is not an issue but distracting.