1

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.

Rai
  • 314
  • 1
  • 2
  • 9
  • 1
    I tested your code on Lua 5.2 and it works fine. What hosting program do you use? – Luke100000 Apr 19 '23 at 05:59
  • 3
    Show your code where you use `type` as an upvalue and try to call it. Most likely you have `type(...)` somewhere in the code *because someone expects type to be the builtin function*. – Luatic Apr 19 '23 at 06:19
  • @Luke100000 It's [Moho](https://mohoscripting.com/), but I don't think it matters since it doesn't use anything simply called "type" in all its scripting interface. I'm not totally sure yet, but I think everything could have more to do with what Luatic says.. – Rai Apr 19 '23 at 13:44
  • @Luatic Ok, so the problem here is "type" may simply not be the kind of _object_ I thought it was? I mean, since it can be used in pure Lua to perform an _action_ (return provided argument kind), I assumed it should be taken by the language as a reserved keyword like in other cases, but it seems type is just different for some reason, isn't? And whether it throw an error or not will depend on if you try to use it as usually after assigning a value... Please, correct me if I'm wrong. But, in any case, where are these kind of _objects_ listed in the manual so one can take care of them? Thanks. – Rai Apr 19 '23 at 13:59
  • 1
    These "objects" are just functions in the global environment. They are listed in [the reference manual](https://www.lua.org/manual/5.1/index.html#index). No sane language defines all of its standard library functions as keywords on a syntactical level. – Luatic Apr 19 '23 at 14:05
  • @Luatic OK; I think I got it... So [HERE](https://www.lua.org/manual/5.1/manual.html#5.1) is where one can get the list of global functions. Well, I should know that and kind of I knew, but I got messed with this "type" word for being a so common word one could want to use as a variable name or whatever and I took as a special case or something... But thanks for letting me know it's just like any other one! – Rai Apr 19 '23 at 14:08
  • @Luatic Yeah (sorry, crossed comment), I think I got it now, as I said, my error was think about this one as another kind of "object" I don't know (now) why . Of course, everything has all the sense the way it is (now). – Rai Apr 19 '23 at 14:12

1 Answers1

5

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.

Luke100000
  • 1,395
  • 2
  • 6
  • 18
  • 1
    Thank you for answering. Now I can see my error by assuming "type" as another kind of object than it actually is, well, just another global function as any other... But now I know for sure why I'd want to avoid to use any of _words_ along the code (even if it's possible as you point) and not simply avoid it just for intuition... Greetings. – Rai Apr 19 '23 at 14:20