"Include" Separate LUA Files

I think a lot of us would like to see some kind of “include” function. In php you can include another php file and its as if that file’s contents were right there in the first file.

Some of us with more complex code need a better way to organize things. Using modules isn’t the same as what I’m describing. fileA.lua included fileB.lua it would be as if all of fileB.lua was actually right in fileA.lua. The code in fileB would interact with code in fileA just like they were the same file. So if you declare local variable123 you could use the variable in fileB–assuming of course it was declared before the “include.”

This way you can slice up your code rather easy. Lets say you have a rather long section of code–say its for your pause menu or something. You could just cut that section of code and paste it into pausemenu.lua. Include the file where the code used to be and your good to go. This would make code much more organized.

The reason I’ve heard this can’t be done is that Apple won’t allow such things for security reasons–everything needs to be in one single file. I don’t understand this explanation at all–this doesn’t seem to be a problem with modules. Who cares if its one big file when it goes to Apple–you could have corona combine the files when the app is built. The purpose of this is just to organize code clearly so the developer isn’t lost in a growing sea of thousands of lines of code.

Anyone else like to see this? Seems like it’d be a very simple thing for Ansca to implement. It seems so basic that I’m shocked that Corona doesn’t already have something like this in place. Hopefully I’m just stupid, it actually does, and one of you can point it out to me. [import]uid: 94309 topic_id: 24609 reply_id: 324609[/import]

There are functions like [lua]dofile[/lua] in lua which pretty much do that.

But Ansca has disabled it for security reasons.
This is not an issue of “everything needs to be in one single file” as you say.
We can dynamically generate code using [lua]dofile[/lua] and THAT obviously compromises security. [import]uid: 64174 topic_id: 24609 reply_id: 99656[/import]

I’m not talking about dynamically generating code on the device. I’m not talking about dofile. What I’m talking about is being able to split up files BEFORE they are built for a device. When Corona builds the app those separate files can then be combined into one and thus there’s no dynamic code generation once the code gets onto the device.

What I’m describing would have 100% no security issues, and Apple wouldn’t even know (or care) whats going on since once it goes onto the iPhone its just one simple file. Is anyone out there picking up what I’m dropping down? [import]uid: 94309 topic_id: 24609 reply_id: 99663[/import]

[lua]require “file”[/lua] [import]uid: 10389 topic_id: 24609 reply_id: 99667[/import]

http://developer.anscamobile.com/reference/index/require [import]uid: 10389 topic_id: 24609 reply_id: 99668[/import]

No require is not what I’m talking about.

You can’t just cut your code, paste it in another file, include the new file and have everything work.

For example:

local coolVariable = 10  
  
local function coolFunction()  
 print (coolVariable)  
end  

Now correct me if I’m wrong, but I’m pretty sure if you cut the above coolFunction and require it the code won’t work. What I want (and I think lots of people would like) is the ability to split up a file without worrying about things like that. This way when a file gets too big and unwieldy you can easily dice it up into different files. Corona would treat these just as one file, and when the app is built it will build the app as if it was just one file.

Not dofile and not require. [import]uid: 94309 topic_id: 24609 reply_id: 99674[/import]

To my understanding this simply isn’t how lua works short of making everything global. In any case it’s a lualang question, not one for the Ansca team per se. But you can accomplish the same thing through requiring.

extend.lua:

local extend = {};  
  
local coolVariable = 10 --private  
extend.coolerVariable = 20 --public  
  
function extend.coolFunction()  
 -- Create and do stuff  
  
 return coolVariable --return result back to your managing file  
end  
return extend;  

main.lua:

local extend = require "extend"  
  
extend.coolFunction()  

What other functionality are you looking for? The only “limitation” here I see is accessing through the namespace, which imo is just even more organized. [import]uid: 87138 topic_id: 24609 reply_id: 99700[/import]

I’m correcting you… You’re wrong.

Your code snippet above would work fine if those were not local declarations.

I don’t know about php, but every language worth it’s salt includes some form of scoping. Lua is no different. Arguing that ‘include’ should work as if local were not there is arguing the toss; there are very good reasons lua does it this way and they are language dependant and not open to Ansca fiddling with. Fog example, in the latest blogged video Walter states that they deliberately stayed away from that sort of work because “we trust in lua’s garbage collector” among others. [import]uid: 8271 topic_id: 24609 reply_id: 99749[/import]

What I’m suggesting shouldn’t affect the scope at all. But doesn’t anyone else get a little frustrated by having a super long lua file. I use require all the time and that helps cut things down some, but I don’t think any of you understand what I’m suggesting.

--file 1  
local variable1 = 1  
local variable 2 = 2  
include ('file2')  
local variable 4 = 4  
  
--file 2  
local varable 3 = 3  
  
--how the Corona simulator sees it   
--and how it gets built as a final app  
  
--file 1  
local variable1 = 1  
local variable 2 = 2  
local variable 3 = 3  
local variable 4 = 4  
  

I don’t see why this would be a problem. It shouldn’t have anything to do with how the code actually gets run, it would just help keep things organized on the programmers side of things. Maybe I’m the only one who doesn’t want code to run on and on. For me it makes it harder to find a line of code that I’m looking for. Doing a good job commenting the code helps, but it would be way faster if I could cut the code up into different files without needing to change scope or things like that. Again, I DON’T want to change the scope of all my functions/variables since I probably scoped things a certain way for a reason.

Would anyone else like such a function that would enable a user to chop up his/her code into several different files? I hear demand for this sort of thing a lot on forums and such, is anyone else interested(or even get what I’m suggesting)? [import]uid: 94309 topic_id: 24609 reply_id: 99800[/import]

To be perfectly honest, the only people I’ve ever heard requesting such functionality are those with very little experience developing. Programming languages are deigned the way they are to make it easy and /logical/ to separate /concerns/. That is what object orientation is about. If you find yourself writing extremely long listings then you are both A) not thinking about the structure carefully enough and B) a functional developer.

Lua is perfectly capable of implementing OOD code and I really suggest you take a look at that.

Also, if you are going to be naming a variable locally then you cannot demand that it is referenced globally. To do what you are suggesting all you need to do is not declare your variables or functions with local. That’s it.

I really think you’re arguing the toss between using two conflicting keywords when you should only be using just one of them.

I also think you’ve misunderstood the use and function of the include keyword as I know of no languages which have it working quite the way you’ve described. [import]uid: 8271 topic_id: 24609 reply_id: 99843[/import]

I’ve done a lot of programming with PHP and in PHP the “include” function works exactly how I described. Check out the description here: http://php.net/manual/en/function.include.php.

The above page explains: “When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.”

This is what I would like to see in Corona. Apparently no one else can see the point of such a function, but in my opinion it would really help organize code and cut down on build time. You could easily break a complicated file of code into several files.

We as LUA programmers do this anyways within the file.

----------------------  
--section one---------  
----------------------  
  
some code  
  
----------------------  
--section two---------  
----------------------  
  
some more code  
  
----------------------  
--another section-----  
----------------------  
  
even more code  
  

Every programmer I’ve ever met does something like what I’ve done above. Wouldn’t it be nice if some of those longer more complicated sections could be moved to a new file? The file could be included and would inherit the scope of the line that includes it.

I guess no one else can see the merit. I think everyone is set in there way of doing things and can see beyond that. I guess instead of pursuing this further I’ll just rework my code so I can use require to accomplish what I’m looking for. Its just annoying to have to set everything as global just so I can use requires when really I don’t want certain things to be global at all. Oh well. [import]uid: 94309 topic_id: 24609 reply_id: 99871[/import]

Why are you making this demand of the Corona developers? They didn’t invent lua.

Further, why are you comparing lua to php? php is a scripting language. lua is not.

I think you will also find that every programmer you’ve ever met does something /similar/ to what you’re describing, but in practice does it a little differently. They will break their code units into separated logical files to encapsulate those functional units.

Every language does things a little differently and what you’re asking for is, in one way, completely available to you in lua. In another way, it does not make sense.

Look through the Corona forums and you’ll see plenty of examples of people requesting things like this. I don’t wish to polarise myself, but I think you’ll find these types of requests being made by those with little or no in-the-field experience. [import]uid: 8271 topic_id: 24609 reply_id: 99873[/import]

I understand what ur saying but don’t see the need for it either require works fine. if it makes it easier for u to have it broke up the. break it up and create a script that puts it all together before u run it.
btw how many lines of code do u have in one file [import]uid: 7911 topic_id: 24609 reply_id: 99898[/import]

@iqsoup

Not sure if this will work to get you what you want but might be worth a look:

http://matthewwild.co.uk/projects/squish/home

[import]uid: 9422 topic_id: 24609 reply_id: 99905[/import]

iqsoup: I think everyone *understands* what you are saying, however their point is simply that this is the way LUA is written.
It would be a bad idea for Ansca to start changing LUA this way.
I have developed in PHP before and I can understand how you would like to include commonly used code as libraries to make it easier to not have to rewrite functionality, but this is not how LUA does it. Your best bet would be to take it up with the LUA authors rather than Ansca :slight_smile: [import]uid: 10389 topic_id: 24609 reply_id: 99918[/import]

Wow–very sorry for making the suggestion…lessons learned!

XenonBL: thanks so much–I’ll check out that link! Glad to have someone willing to help me out.

horacebury: So sorry–I didn’t mean compare lua and php. I was trying to explain what I meant and the only way I knew how to do that was using an example from another language. I know they are different languages, you don’t need to point that out. And are you suggesting that I have “little or no in-the-field experience?” We are all learning here and unless you invented LUA please keep comments like that to yourself. Sorry for making a well meaning suggestion, it won’t happen again. [import]uid: 94309 topic_id: 24609 reply_id: 99940[/import]

Ok, first off, sorry from me too - yesterday was not a good day for me and you appeared to be quite ardently defensive about something which Ansca really have no right in messing with.

I don’t think you need to be sorry about making the suggestion, though it needed to be more of a discussion.

And don’t be afraid to raise subjects - I do think this community is a great place for that and for learning.

And no, I was not implying that you have little or not field experience - it was just a comparison from the past. [import]uid: 8271 topic_id: 24609 reply_id: 100018[/import]