Require any string?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Require any string?

Post by T-Bone »

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

Code: Select all

require 'map2.lua'
it works great. However, if I want to run map number 'mapNbr', it doesn't work.

Code: Select all

require 'map'..mapNbr..'.lua'
does not work. I get an error

Code: Select all

unexpected symbol near '..'
. Any workaround? I really don't want to do like

Code: Select all

if lvlNbr==1 then require map1.lua 
elseif lvlNbr==2 then require map2.lua
Any ideas? :neko:
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Require any string?

Post by bartbes »

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:

Code: Select all

require("map" .. mapNbr)
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Require any string?

Post by T-Bone »

I had no idea you could use require with parenthesis. Lulz. Thanks :neko:

And I promise to be a good boy and stop writing .lua in my require functions :neko:
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Require any string?

Post by BlackBulletIV »

T-Bone wrote:I had no idea you could use require with parenthesis.
Yeah, it's not a statement. Even if it was, you could still use parenthesis as an expression. For example, this works fine:

Code: Select all

if (hello == 3) then ... end
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Require any string?

Post by Kadoba »

I think this sort of shortcut is limited by a single literal string. It also works with any function.

Code: Select all

print "hi"             -- works
print( "hi" )          -- works
print 4                -- doesn't work
print( 4 )             -- works
print "hi", "there"    -- doesn't work
print( "hi", "there" ) -- works
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Require any string?

Post by bartbes »

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?
User avatar
XQYZ
Prole
Posts: 20
Joined: Sat Jul 30, 2011 4:23 pm
Location: Europe
Contact:

Re: Require any string?

Post by XQYZ »

bartbes wrote:
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.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Require any string?

Post by Kadoba »

XQYZ wrote: What if you assign it to a variable and then use that?
No. It's not a literal anymore then. And yes I agree it's more confusing than useful.
bartbes wrote: Single is quite redundant, as soon as you start concatenating them it no longer is a literal string, is it?
I mean you can't do this:

Code: Select all

 print "one", "two" 
User avatar
slime
Solid Snayke
Posts: 3133
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Require any string?

Post by slime »

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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Require any string?

Post by bartbes »

Kadoba wrote: I mean you can't do this:

Code: Select all

 print "one", "two" 
Not. A. Literal.

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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 64 guests