A:Inc(5) is treated like A.Inc(A,5).
where A is an object which has the Inc function and Inc expects two arguments (an object and an integer).
---
function T:F(X) is equivalent to
function T.F(self, X) which is equivalent to
T.F = function (self, X)
Monday, January 21, 2008
Thursday, January 17, 2008
The following two tables are equivalent:
NTI = {["John"] = "rhythm guitar"}
NTI = {John = "rhythm guitar"}
if the key is a valid identifier, it can be written out without the [" "] around it.
ipairs loops through arrays
pairs loops through non-arrays (and loops through in an arbitrary order)
functions inside an object are called methods.
NTI = {["John"] = "rhythm guitar"}
NTI = {John = "rhythm guitar"}
if the key is a valid identifier, it can be written out without the [" "] around it.
ipairs loops through arrays
pairs loops through non-arrays (and loops through in an arbitrary order)
functions inside an object are called methods.
Wednesday, January 16, 2008
Tables
"If a function call is used as the value of an explicit key ({K = F()}, for example), it's adjusted to one return value. If it's used as the value of an implicit integer key, it's only adjusted to one return value if it's not the last thing in the table constructor; if it's the last thing, no adjustment is made."
> function ReturnNothing ()
>> end
> function ReturnThreeVals ()
>> return "x", "y", "z"
>> end
> TblA = {ReturnThreeVals (), ReturnThreeVals()}
> print (TblA[1], TblA[2], TblA[3], TblA[4])
x x y z
> TblC = {ReturnThreeVals (), ReturnNothing ()}
> print (TblC[1], TblC[2], TblC[3], TblC[4])
x nil nil nil
TblC[2] is nil because it is set to the result of ReturnNothing (). 3 and 4 are nil because they were not set.
If a table has a "gap" (a nil in element 1 or later, but not all elements are nil) then the length operator "#" is not defined.
> function ReturnNothing ()
>> end
> function ReturnThreeVals ()
>> return "x", "y", "z"
>> end
> TblA = {ReturnThreeVals (), ReturnThreeVals()}
> print (TblA[1], TblA[2], TblA[3], TblA[4])
x x y z
> TblC = {ReturnThreeVals (), ReturnNothing ()}
> print (TblC[1], TblC[2], TblC[3], TblC[4])
x nil nil nil
TblC[2] is nil because it is set to the result of ReturnNothing (). 3 and 4 are nil because they were not set.
If a table has a "gap" (a nil in element 1 or later, but not all elements are nil) then the length operator "#" is not defined.
Common bugs - global instead of local
"There are two ways to accidentally use a global when a local was intended. One is to misspell the name of a local variable. The other is to forget the local keyword. Both of these are common sources of bugs."
"If you are ever in doubt about the scope of a variable, start from the statement where the variable's name is used and search upwards for the following:
- A local statement that creates a variable of this name or a local function statement that creates a function of this name.
- A function definition (a function statement, local function statement, or function expression) that uses this name as a formal argument
- A for loop that uses this name as a loop variable
The first one of these that you run into whose scope extends to the statement where you started searching is the place where your (local) variable was created. If your search hits the top of the file without finding anything, the variable is global."
"If you are ever in doubt about the scope of a variable, start from the statement where the variable's name is used and search upwards for the following:
- A local statement that creates a variable of this name or a local function statement that creates a function of this name.
- A function definition (a function statement, local function statement, or function expression) that uses this name as a formal argument
- A for loop that uses this name as a loop variable
The first one of these that you run into whose scope extends to the statement where you started searching is the place where your (local) variable was created. If your search hits the top of the file without finding anything, the variable is global."
Upvalues
From Beginning Lua Programming:
"When a function uses a variable that is local to a containing scope ... that variable is called an external local variable or an upvalue. ... A function that has one or more upvalues is called a closure." In the following code N is an upvalue.
> function MakeLessThan(N)
>> return function (X)
>> return X < N
>> end
> end
> LessThanFive = MakeLessThan(5)
> print (LessThanFive(4))
true
You can also call a function directly, without giving it a name.
> print (MakeLessThan(5) (4))
true
"When a function uses a variable that is local to a containing scope ... that variable is called an external local variable or an upvalue. ... A function that has one or more upvalues is called a closure." In the following code N is an upvalue.
> function MakeLessThan(N)
>> return function (X)
>> return X < N
>> end
> end
> LessThanFive = MakeLessThan(5)
> print (LessThanFive(4))
true
You can also call a function directly, without giving it a name.
> print (MakeLessThan(5) (4))
true
Lua local functions
In the following snippet, the "end)(2,3)" need to be on the same line for the code to work. Not sure why, maybe Lua interprets the (2,3) on a separate line as a possible function call. The error call says "ambiguous syntax".
> function(A,B)
>> print(A+B)
>> end)(2,3)
The above works, the below doesn't.
>function(A,B)
>> print(A+B)
>> end)
>> (2,3)
According to Beginning Lua Programming "... Lua doesn't allow a newline before the open parenthesis of a function call. [page 107]"
> function(A,B)
>> print(A+B)
>> end)(2,3)
The above works, the below doesn't.
>function(A,B)
>> print(A+B)
>> end)
>> (2,3)
According to Beginning Lua Programming "... Lua doesn't allow a newline before the open parenthesis of a function call. [page 107]"
Thursday, January 10, 2008
Return is not a function
> return ReturnArgs(1,2,3)
1, 2, 3
> return (ReturnArgs(1,2,3))
1
wrapping what follows return in parentheses forces only a single argument out of the ReturnArgs function.
1, 2, 3
> return (ReturnArgs(1,2,3))
1
wrapping what follows return in parentheses forces only a single argument out of the ReturnArgs function.
A little bit of Lua weirdness
Quoting from Beginning Lua Programming:
"If a function call is the last (or only) expression in a value list, then all (if any) values returned by the function are used. If a function call is in a value list but is not the last expression, then its first return value (or nil if it returns nothing) is used and any remaining return values are discarded."
So, in the case of function Do Nothing () end:
> print(1, DoNothing())
1
> print (DoNothing(), 2)
nil
and
> print (ReturnArgs(1,2,3), ReturnArgs("a", "b", "c"))
1 a b c
"If a function call is the last (or only) expression in a value list, then all (if any) values returned by the function are used. If a function call is in a value list but is not the last expression, then its first return value (or nil if it returns nothing) is used and any remaining return values are discarded."
So, in the case of function Do Nothing () end:
> print(1, DoNothing())
1
> print (DoNothing(), 2)
nil
and
> print (ReturnArgs(1,2,3), ReturnArgs("a", "b", "c"))
1 a b c
Lua reading
Working my way through the Lua language. It turns out that Lua only has 8 types: booleans, nils, numbers, strings, functions, tables, threads and userdata.
Tuesday, January 8, 2008
Everex
Just got a new gPC from Everex and couldn't be more pleased. It's amazing how good a computer it is for under $200. And it has GCC and Python pre-loaded, which made me very happy. So long Cygwin! Luckily, I know some UNIX or installing Lua would have been a pain. I'm just so happy that everything works.
Next step: power on my Windows PC and this new Linux box and compare how long it takes each to boot.
Next step: power on my Windows PC and this new Linux box and compare how long it takes each to boot.
Subscribe to:
Posts (Atom)