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