From 6c53c05dcc25e74885411832839aa19ff46ee66a Mon Sep 17 00:00:00 2001 From: Rob Hoelz Date: Wed, 4 Sep 2013 11:39:15 +0200 Subject: Clarify self-referential local functions The current language used implies that `local function f() ... end` does not allow f to invoke itself. This has been clarified via the addition of an example of a local function that may *not* invoke itself. --- lua.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lua.html.markdown') diff --git a/lua.html.markdown b/lua.html.markdown index 7325a1cf..35d68e9b 100644 --- a/lua.html.markdown +++ b/lua.html.markdown @@ -125,6 +125,9 @@ f = function (x) return x * x end -- And so are these: local function g(x) return math.sin(x) end +local g = function(x) return math.xin(x) end +-- Equivalent to local function g(x)..., except referring +-- to g in the function body won't work as expected. local g; g = function (x) return math.sin(x) end -- the 'local g' decl makes g-self-references ok. -- cgit v1.2.3 From 57e0ac7e95a55ede7408a36dc7524649bbc8e4c2 Mon Sep 17 00:00:00 2001 From: Rob Hoelz Date: Wed, 4 Sep 2013 11:41:42 +0200 Subject: Add another example of function call without parens Function calls using a table literal as its sole parameter may also omit parentheses --- lua.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lua.html.markdown') diff --git a/lua.html.markdown b/lua.html.markdown index 35d68e9b..9f9fd77b 100644 --- a/lua.html.markdown +++ b/lua.html.markdown @@ -136,6 +136,10 @@ local g; g = function (x) return math.sin(x) end -- Calls with one string param don't need parens: print 'hello' -- Works fine. +-- Calls with one table param don't need parens +-- either (more on tables below): +print {} -- Works fine too. + ---------------------------------------------------- -- 3. Tables. -- cgit v1.2.3 From edea434d89049b700a58f8523fbd57590de5855d Mon Sep 17 00:00:00 2001 From: Rob Hoelz Date: Wed, 4 Sep 2013 11:43:09 +0200 Subject: Use local variables for function examples Currently, the example functions create variables in global scope. --- lua.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lua.html.markdown') diff --git a/lua.html.markdown b/lua.html.markdown index 9f9fd77b..369de908 100644 --- a/lua.html.markdown +++ b/lua.html.markdown @@ -210,7 +210,7 @@ f2 = {a = 2, b = 3} metafraction = {} function metafraction.__add(f1, f2) - sum = {} + local sum = {} sum.b = f1.b * f2.b sum.a = f1.a * f2.b + f2.a * f1.b return sum @@ -273,7 +273,7 @@ eatenBy = myFavs.animal -- works! thanks, metatable Dog = {} -- 1. function Dog:new() -- 2. - newObj = {sound = 'woof'} -- 3. + local newObj = {sound = 'woof'} -- 3. self.__index = self -- 4. return setmetatable(newObj, self) -- 5. end @@ -308,7 +308,7 @@ mrDog:makeSound() -- 'I say woof' -- 8. LoudDog = Dog:new() -- 1. function LoudDog:makeSound() - s = self.sound .. ' ' -- 2. + local s = self.sound .. ' ' -- 2. print(s .. s .. s) end @@ -329,7 +329,7 @@ seymour:makeSound() -- 'woof woof woof' -- 4. -- If needed, a subclass's new() is like the base's: function LoudDog:new() - newObj = {} + local newObj = {} -- set up newObj self.__index = self return setmetatable(newObj, self) -- cgit v1.2.3 From 8cbabf82b8ff938907ad6fc86327706668544c89 Mon Sep 17 00:00:00 2001 From: Craig Roddin Date: Mon, 7 Oct 2013 12:33:10 -0600 Subject: Fixed spelling of trig function "sin" --- lua.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua.html.markdown') diff --git a/lua.html.markdown b/lua.html.markdown index 369de908..27ce105b 100644 --- a/lua.html.markdown +++ b/lua.html.markdown @@ -125,7 +125,7 @@ f = function (x) return x * x end -- And so are these: local function g(x) return math.sin(x) end -local g = function(x) return math.xin(x) end +local g = function(x) return math.sin(x) end -- Equivalent to local function g(x)..., except referring -- to g in the function body won't work as expected. local g; g = function (x) return math.sin(x) end -- cgit v1.2.3 From f70b1edcfbe14bd41fb7c1e45113359a3c75b4e3 Mon Sep 17 00:00:00 2001 From: Jakukyo Friel Date: Thu, 6 Feb 2014 01:45:16 +0800 Subject: lua: typo `LoudDog.__index` is `Dog`, not `LoudDog`. --- lua.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua.html.markdown') diff --git a/lua.html.markdown b/lua.html.markdown index 27ce105b..bdd59999 100644 --- a/lua.html.markdown +++ b/lua.html.markdown @@ -321,7 +321,7 @@ seymour:makeSound() -- 'woof woof woof' -- 4. -- Dog.new(LoudDog) as LoudDog has no 'new' key, -- but does have __index = Dog on its metatable. -- Result: seymour's metatable is LoudDog, and --- LoudDog.__index = LoudDog. So seymour.key will +-- LoudDog.__index = Dog. So seymour.key will -- = seymour.key, LoudDog.key, Dog.key, whichever -- table is the first with the given key. -- 4. The 'makeSound' key is found in LoudDog; this -- cgit v1.2.3 From 88274f8a3af0c270b1580b561e6f1c0a8dfbd3ec Mon Sep 17 00:00:00 2001 From: iirelu Date: Mon, 3 Mar 2014 15:49:12 +0000 Subject: normalised lua doc to 80 chars per line When this was originally written, it was written for a line-width of 50 characters, however Learn X in Y Minutes' site is designed for 80-character widths. This made it look very out of place. --- lua.html.markdown | 151 +++++++++++++++++++++++++----------------------------- 1 file changed, 69 insertions(+), 82 deletions(-) (limited to 'lua.html.markdown') diff --git a/lua.html.markdown b/lua.html.markdown index bdd59999..be9f3141 100644 --- a/lua.html.markdown +++ b/lua.html.markdown @@ -12,15 +12,13 @@ filename: learnlua.lua Adding two ['s and ]'s makes it a multi-line comment. --]] - ----------------------------------------------------- +-------------------------------------------------------------------------------- -- 1. Variables and flow control. ----------------------------------------------------- +-------------------------------------------------------------------------------- num = 42 -- All numbers are doubles. --- Don't freak out, 64-bit doubles have 52 bits for --- storing exact int values; machine precision is --- not a problem for ints that need < 52 bits. +-- Don't freak out, 64-bit doubles have 52 bits for storing exact int +-- values; machine precision is not a problem for ints that need < 52 bits. s = 'walternate' -- Immutable strings like Python. t = "double-quotes are also fine" @@ -60,8 +58,8 @@ aBoolValue = false -- Only nil and false are falsy; 0 and '' are true! if not aBoolValue then print('twas false') end --- 'or' and 'and' are short-circuited. --- This is similar to the a?b:c operator in C/js: +-- 'or' and 'and' are short-circuited. This is similar to the a?b:c operator +-- in C/js: ans = aBoolValue and 'yes' or 'no' --> 'no' karlSum = 0 @@ -81,10 +79,9 @@ repeat num = num - 1 until num == 0 - ----------------------------------------------------- +-------------------------------------------------------------------------------- -- 2. Functions. ----------------------------------------------------- +-------------------------------------------------------------------------------- function fib(n) if n < 2 then return n end @@ -93,8 +90,8 @@ end -- Closures and anonymous functions are ok: function adder(x) - -- The returned function is created when adder is - -- called, and remembers the value of x: + -- The returned function is created when adder is called, and remembers the + -- value of x: return function (y) return x + y end end a1 = adder(9) @@ -102,10 +99,9 @@ a2 = adder(36) print(a1(16)) --> 25 print(a2(64)) --> 100 --- Returns, func calls, and assignments all work --- with lists that may be mismatched in length. --- Unmatched receivers are nil; --- unmatched senders are discarded. +-- Returns, func calls, and assignments all work with lists that may be +-- mismatched in length. Unmatched receivers are nil; unmatched senders are +-- discarded. x, y, z = 1, 2, 3, 4 -- Now x = 1, y = 2, z = 3, and 4 is thrown away. @@ -118,16 +114,15 @@ end x, y = bar('zaphod') --> prints "zaphod nil nil" -- Now x = 4, y = 8, values 15..42 are discarded. --- Functions are first-class, may be local/global. --- These are the same: +-- Functions are first-class, may be local/global. These are the same: function f(x) return x * x end f = function (x) return x * x end -- And so are these: local function g(x) return math.sin(x) end local g = function(x) return math.sin(x) end --- Equivalent to local function g(x)..., except referring --- to g in the function body won't work as expected. +-- Equivalent to local function g(x)..., except referring to g in the function +-- body won't work as expected. local g; g = function (x) return math.sin(x) end -- the 'local g' decl makes g-self-references ok. @@ -136,19 +131,16 @@ local g; g = function (x) return math.sin(x) end -- Calls with one string param don't need parens: print 'hello' -- Works fine. --- Calls with one table param don't need parens --- either (more on tables below): +-- Calls with one table param don't need parens either (more on tables below): print {} -- Works fine too. - ----------------------------------------------------- +-------------------------------------------------------------------------------- -- 3. Tables. ----------------------------------------------------- +-------------------------------------------------------------------------------- --- Tables = Lua's only compound data structure; --- they are associative arrays. --- Similar to php arrays or js objects, they are --- hash-lookup dicts that can also be used as lists. +-- Tables = Lua's only compound data structure; they are associative arrays. +-- Similar to php arrays or js objects, they are hash-lookup dicts that can +-- also be used as lists. -- Using tables as dictionaries / maps: @@ -164,14 +156,13 @@ t.key2 = nil -- Removes key2 from the table. u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'} print(u[6.28]) -- prints "tau" --- Key matching is basically by value for numbers --- and strings, but by identity for tables. +-- Key matching is basically by value for numbers and strings, but by identity +-- for tables. a = u['@!#'] -- Now a = 'qbert'. b = u[{}] -- We might expect 1729, but it's nil: --- b = nil since the lookup fails. It fails --- because the key we used is not the same object --- as the one used to store the original value. So --- strings & numbers are more portable keys. +-- b = nil since the lookup fails. It fails because the key we used is not the +-- same object as the one used to store the original value. So strings & +-- numbers are more portable keys. -- A one-table-param function call needs no parens: function h(x) print(x.key1) end @@ -191,16 +182,15 @@ v = {'value1', 'value2', 1.21, 'gigawatts'} for i = 1, #v do -- #v is the size of v for lists. print(v[i]) -- Indices start at 1 !! SO CRAZY! end --- A 'list' is not a real type. v is just a table --- with consecutive integer keys, treated as a list. +-- A 'list' is not a real type. v is just a table with consecutive integer +-- keys, treated as a list. ----------------------------------------------------- +-------------------------------------------------------------------------------- -- 3.1 Metatables and metamethods. ----------------------------------------------------- +-------------------------------------------------------------------------------- --- A table can have a metatable that gives the table --- operator-overloadish behavior. Later we'll see --- how metatables support js-prototypey behavior. +-- A table can have a metatable that gives the table operator-overloadish +-- behavior. Later we'll see how metatables support js-prototypey behavior. f1 = {a = 1, b = 2} -- Represents the fraction a/b. f2 = {a = 2, b = 3} @@ -221,10 +211,9 @@ setmetatable(f2, metafraction) s = f1 + f2 -- call __add(f1, f2) on f1's metatable --- f1, f2 have no key for their metatable, unlike --- prototypes in js, so you must retrieve it as in --- getmetatable(f1). The metatable is a normal table --- with keys that Lua knows about, like __add. +-- f1, f2 have no key for their metatable, unlike prototypes in js, so you must +-- retrieve it as in getmetatable(f1). The metatable is a normal table with +-- keys that Lua knows about, like __add. -- But the next line fails since s has no metatable: -- t = s + s @@ -236,11 +225,12 @@ myFavs = {food = 'pizza'} setmetatable(myFavs, {__index = defaultFavs}) eatenBy = myFavs.animal -- works! thanks, metatable --- Direct table lookups that fail will retry using --- the metatable's __index value, and this recurses. +-------------------------------------------------------------------------------- +-- Direct table lookups that fail will retry using the metatable's __index +-- value, and this recurses. --- An __index value can also be a function(tbl, key) --- for more customized lookups. +-- An __index value can also be a function(tbl, key) for more customized +-- lookups. -- Values of __index,add, .. are called metamethods. -- Full list. Here a is a table with the metamethod. @@ -261,12 +251,12 @@ eatenBy = myFavs.animal -- works! thanks, metatable -- __newindex(a, b, c) for a.b = c -- __call(a, ...) for a(...) ----------------------------------------------------- +-------------------------------------------------------------------------------- -- 3.2 Class-like tables and inheritance. ----------------------------------------------------- +-------------------------------------------------------------------------------- --- Classes aren't built in; there are different ways --- to make them using tables and metatables. +-- Classes aren't built in; there are different ways to make them using +-- tables and metatables. -- Explanation for this example is below it. @@ -286,22 +276,20 @@ mrDog = Dog:new() -- 7. mrDog:makeSound() -- 'I say woof' -- 8. -- 1. Dog acts like a class; it's really a table. --- 2. function tablename:fn(...) is the same as --- function tablename.fn(self, ...) --- The : just adds a first arg called self. --- Read 7 & 8 below for how self gets its value. +-- 2. "function tablename:fn(...)" is the same as +-- "function tablename.fn(self, ...)", The : just adds a first arg called +-- self. Read 7 & 8 below for how self gets its value. -- 3. newObj will be an instance of class Dog. --- 4. self = the class being instantiated. Often --- self = Dog, but inheritance can change it. --- newObj gets self's functions when we set both --- newObj's metatable and self's __index to self. +-- 4. "self" is the class being instantiated. Often self = Dog, but inheritance +-- can change it. newObj gets self's functions when we set both newObj's +-- metatable and self's __index to self. -- 5. Reminder: setmetatable returns its first arg. --- 6. The : works as in 2, but this time we expect --- self to be an instance instead of a class. +-- 6. The : works as in 2, but this time we expect self to be an instance +-- instead of a class. -- 7. Same as Dog.new(Dog), so self = Dog in new(). -- 8. Same as mrDog.makeSound(mrDog); self = mrDog. ----------------------------------------------------- +-------------------------------------------------------------------------------- -- Inheritance example: @@ -315,17 +303,16 @@ end seymour = LoudDog:new() -- 3. seymour:makeSound() -- 'woof woof woof' -- 4. +-------------------------------------------------------------------------------- -- 1. LoudDog gets Dog's methods and variables. -- 2. self has a 'sound' key from new(), see 3. --- 3. Same as LoudDog.new(LoudDog), and converted to --- Dog.new(LoudDog) as LoudDog has no 'new' key, --- but does have __index = Dog on its metatable. --- Result: seymour's metatable is LoudDog, and --- LoudDog.__index = Dog. So seymour.key will --- = seymour.key, LoudDog.key, Dog.key, whichever +-- 3. Same as "LoudDog.new(LoudDog)", and converted to "Dog.new(LoudDog)" as +-- LoudDog has no 'new' key, but does have "__index = Dog" on its metatable. +-- Result: seymour's metatable is LoudDog, and "LoudDog.__index = Dog". So +-- seymour.key will equal seymour.key, LoudDog.key, Dog.key, whichever -- table is the first with the given key. --- 4. The 'makeSound' key is found in LoudDog; this --- is the same as LoudDog.makeSound(seymour). +-- 4. The 'makeSound' key is found in LoudDog; this is the same as +-- "LoudDog.makeSound(seymour)". -- If needed, a subclass's new() is like the base's: function LoudDog:new() @@ -335,13 +322,13 @@ function LoudDog:new() return setmetatable(newObj, self) end ----------------------------------------------------- +-------------------------------------------------------------------------------- -- 4. Modules. ----------------------------------------------------- +-------------------------------------------------------------------------------- ---[[ I'm commenting out this section so the rest of --- this script remains runnable. +--[[ I'm commenting out this section so the rest of this script remains +-- runnable. ``` ```lua @@ -367,8 +354,8 @@ local mod = require('mod') -- Run the file mod.lua. local mod = (function () end)() --- It's like mod.lua is a function body, so that --- locals inside mod.lua are invisible outside it. +-- It's like mod.lua is a function body, so that locals inside mod.lua are +-- invisible outside it. -- This works because mod here = M in mod.lua: mod.sayHello() -- Says hello to Hrunkner. @@ -376,8 +363,8 @@ mod.sayHello() -- Says hello to Hrunkner. -- This is wrong; sayMyName only exists in mod.lua: mod.sayMyName() -- error --- require's return values are cached so a file is --- run at most once, even when require'd many times. +-- require's return values are cached so a file is run at most once, even when +-- require'd many times. -- Suppose mod2.lua contains "print('Hi!')". local a = require('mod2') -- Prints Hi! -- cgit v1.2.3 From 6933d24227ec5a0f220bf897e57d5222eaba0084 Mon Sep 17 00:00:00 2001 From: Geoffrey Liu Date: Sun, 12 Oct 2014 12:24:52 -0700 Subject: Capitalize language names. See #137 --- lua.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lua.html.markdown') diff --git a/lua.html.markdown b/lua.html.markdown index be9f3141..0809215f 100644 --- a/lua.html.markdown +++ b/lua.html.markdown @@ -1,5 +1,5 @@ --- -language: lua +language: Lua contributors: - ["Tyler Neylon", "http://tylerneylon.com/"] filename: learnlua.lua -- cgit v1.2.3