Lua : some basics

This is an extension of my last post about Lua. If you have not read that yet, I strongly suggest start from there as a beginner of Lua. On the other hand, if you know the basics of Lua, this post is exactly for you.
Hope you enjoy Lua as I am excited about it. Lets get started.
Commenting in Lua is done by “–” for single line and with “–[[ ]]” for multiline.

-- single line comment
--[[
Multiline comment is
Like this.
]]
print("comments are done, get back to work")

Lua code can be decided into specific modules. If we want to put all of a similar kind of functionality into one .lua file then we can call that file as a Lua module. Suppose we make a file named “package2com.lua” to store some kind of similar functionalities and store the file inside a folder named “com” . Now we need to call this new module (which we recently created “package2com”) inside our main Lua class, then the code inside main.lua will look as below

 require("com.package2com") 

Just remember that, while using “require”, the module name does not include “.lua” in it.
Now writing a module is as simple as the below declaration in the beginning of the module file.

module(...,package.seeall)

And then defining the variables and functions after that.
There are some functions in Lua that take “self” as the first parameter. We can also write functions which take “self” as first parameter.

function trace(self,message)
print('self parameter')
end

Calling a module function is as simple as using a . (dot) after module name and then putting the function name. If we want to call a function present in our “package2com” module, the code will look as

com.package2com.myfunction()

The exception is to call functions which take “self” as first parameter. There the call will happen through “:” not “.” , the example code is

com.package2com:mySelfFunction()

If the function takes “self” as first parameter, we need not pass self from the point we are calling the function, rather call is made with “:” and Lua takes care of the rest.
Local functions are not available outside that module.
Defining more than one variables is, possible in one declaration as below

local x,y,z=4,"string value",5

If we do not specify all the values for all the variables, a value of “nil” is applied to that variable. Similarly, if more values are provided than the variables, then they are ignored.
Type conversion is automatic in Lua. The code below

print (20+'4')

will result in 24.
But if we say

print ('string'+4)

That will throw an error, saying “attempt to perform arithmetic on a string value”. That means, the type conversion will be automatic for numeric values wrapped in string.
The string concatenation is done with “..” as shown below

print ('4'..'5')

Lua!!
The final example codes are attached here for your reference, there are code comments and result comments also, which can be handy to understand some of the concepts described here with more clarity.

main.lua

-- Module study
--Program entry
require("com.package2com");

com.package2com:trace('what');
com.package2com.traceM('what');
-- com.package2com:traceModeIsLocal(); --for its local scoped

print(os.date());
print(os.time());

--[[
 form3.wlua
 require 'CLRPackage'
 import "System.Windows.Forms"
 import "System.Drawing"

 button = Button()
 button.Text = "Click!"
 button.Dock = DockStyle.Top
 edit = RichTextBox()
 edit.Dock = DockStyle.Fill

 form = Form()
 form.Text = "Hello, World!"
 form.Controls:Add(edit)
 form.Controls:Add(button)
 form:ShowDialog()
 ]]

print('Comments are ended!!')

local x,y,z=1,'second variable value',3,4;
print(x,y,z);

local x,y,z=4,5;
print(x,y,z);

print(x+y);
--print(x+y+z);
--[[
Will result in an error like this :
    error: attempt to perform arithmetic on global 'c' ( a nil value)

nil is also the only concept of "false-hood" Lua supports. We've seen this in the upper example. It's also good to remind that nil is false but not equal to 0 in a numeric sense. This was why

    print(a + b + c)

c being a nil "value" will result in a runtime error
]]

print('Thats awesome, Lua.');
print(20+'4');
print('4'+20);
--print('20'+' string');
print ('20'+'4');
print ('20'..'4');--string concatenation
--string study
local s=4;
local t=5;
print('Interesting to see the same variables can produce different results depending upon operators!');
print(s..t); -- concatenation
print(s+t);  -- addition

package2com.lua (which is inside com folder)

module(...,package.seeall);

local i=5;
local n='what ?';

local function traceModeIsLocal()
	print('---------------Local Mode-------------------');
end

--variables and methods must be defined before they are called

function trace(self,message)
	print('-------------GLOBAL---------------------');
	print('---- Called with : not . -----');
	print(_VERSION);
	print(message);
	print(i);
	print(n);
	traceModeIsLocal();
	print('When self is the first parameter, the functions are called with a : ');
	print('----------------------------------');

end

function traceM(message)
	print('-------------GLOBAL---------------------');
	print('---- Called with . not : -----');
	print(_VERSION);
	print(message);
	traceModeIsLocal();
	print('----------------------------------');
end

Thats all to it, we can now start building our modular Lua applications.

Posted from WordPress for Android through my “HTC Wildfire”.