10 - <https://www.lua.org/pil/13.html>
11 - <http://lua-users.org/wiki/ObjectOrientedProgramming>
19 ### Invoking functions
24 -- You can omit parentheses if the argument is one string or table literal
25 print "Hello World" <--> print("Hello World")
26 dofile 'a.lua' <--> dofile ('a.lua')
27 print [[a multi-line <--> print([[a multi-line
29 f{x=10, y=20} <--> f({x=10, y=20})
36 t.a = function() ... end
38 t = { ["hello"] = 200 }
41 -- Remember, arrays are also tables
42 array = { "a", "b", "c", "d" }
43 print(array[2]) -- "b" (one-indexed)
44 print(#array) -- 4 (length)
54 for i = start,finish,delta do
57 for k,v in pairs(tab) do
65 if condition then break end
89 function myFunctionWithArgs(a, b)
95 anonymousFunctions(function()
99 -- Not exported in the module
100 local function myPrivateFunction()
104 function doAction(action, ...)
105 print("Doing '"..action.."' to", ...)
106 --> print("Doing 'write' to", "Shirley", "Abed")
109 doAction('write', "Shirley", "Abed")
113 mytable = { x = 2, y = function() .. end }
119 -- Syntactic sugar, these are equivalent:
123 mytable.y(mytable, a, b)
126 function X:y(z) .. end
127 function X.y(self, z) .. end
135 -- A metatable is simply a table with functions in it.
136 mt.__tostring = function() return "lol" end
137 mt.__add = function(b) ... end -- a + b
138 mt.__mul = function(b) ... end -- a * b
139 mt.__index = function(k) ... end -- Lookups (a[k] or a.k)
140 mt.__newindex = function(k, v) ... end -- Setters (a[k] = v)
142 -- Metatables allow you to override behavior of another table.
144 setmetatable(mytable, mt)
152 function Account:new(balance)
153 local t = setmetatable({}, { __index = Account })
155 -- Your constructor stuff
156 t.balance = (balance or 0)
160 function Account:withdraw(amount)
161 print("Withdrawing "..amount.."...")
162 self.balance = self.balance - amount
166 function Account:report()
167 print("Your current balance is: "..self.balance)
170 a = Account:new(9000)
171 a:withdraw(200) -- method call
179 ## Operators (and their metatable names)
183 -- Relational (binary)
184 -- __eq __lt __gt __le __ge
186 ~= -- Not equal, just like !=
188 -- Arithmetic (binary)
189 -- __add __sub __muv __div __mod __pow
192 -- Arithmetic (unary)
193 -- __unm (unary minus)
199 nil and false --> nil
200 false and nil --> false
212 -- __index(table, key)
216 -- __newindex(table, key, value)
220 -- __concat(left, right)
228 ### API: Global Functions
231 loadfile("hello.lua")
233 assert(x) -- x or (raise an error)
236 type(var) -- "nil" | "number" | "string" | "boolean" | "table" | "function" | "thread" | "userdata"
238 -- Does /not/ invoke meta methods (__index and __newindex)
239 rawset(t, index, value) -- Like t[index] = value
240 rawget(t, index) -- Like t[index]
243 setfenv(1, {}) -- 1: current function, 2: caller, and so on -- {}: the new _G
245 pairs(t) -- iterable list of {key, value}
246 ipairs(t) -- iterable list of {index, value}
253 'string'..'concatenation'
258 s:len() -- Just like #s
278 table.foreach(t, function(row) ... end)
280 table.insert(t, 21) -- append (--> t[#t+1] = 21)
281 table.insert(t, 4, 99)
289 math.abs math.acos math.asin math.atan math.atan2
290 math.ceil math.cos math.cosh math.deg math.exp
291 math.floor math.fmod math.frexp math.ldexp math.log
292 math.log10 math.max math.min math.modf math.pow
293 math.rad math.random math.randomseed math.sin math.sinh
294 math.sqrt math.tan math.tanh
301 io.output(io.open("file.txt", "w"))
305 for line in io.lines("file.txt")
307 file = assert(io.open("file.txt", "r"))