I've had some issues with the require function. My game creates .lua files for game maps, and I want to run them using the require function. The .lua files are named map1.lua, map2.lua and so on. If you specify which one you want, like this
Because the parenthesis-less invocation only works with literals, not with variables. Require is a function like any other.
I will also note that the suffix ".lua" is wrong and deprecated, and is removed for 0.8.0.
So you'll end up with:
Kadoba wrote:I think this sort of shortcut is limited by a single literal string. It also works with any function.
Single is quite redundant, as soon as you start concatenating them it no longer is a literal string, is it?
What if you assign it to a variable and then use that?
IMHO this whole thing is just inconsistent and Lua should require the brackets all the time. Python had a similar thing in Version 2 where the print command didn't require brackets like all other functions and they dropped that in Version 3 in favor of consistency.
So they should either do that or do it like PHP with their echo command and make it possible to concat strings and variables.
Right, the shortcut only works for a single argument (a literal string or table constructor).
From Programming In Lua:
In both cases, we write a list of arguments enclosed in parentheses. If the function call has no arguments, we must write an empty list () to indicate the call. There is a special case to this rule: If the function has one single argument and this argument is either a literal string or a table constructor, then the parentheses are optional:
Last edited by slime on Tue Aug 02, 2011 5:32 pm, edited 2 times in total.
Also, this is slightly different from your python print (which wasn't a function!), because this works for all functions. ALL.
Same goes for tables, if the only argument is a literal table, you can skip the parentheses (makes a lot more sense than for strings, but whatever).
slime wrote:Right, the shortcut only works for a single argument (a literal string or literal table).
As I said, single literal makes no sense. If you start doing 'operations' on it, it is no longer a literal. It just isn't.