New to Lua Question

Hi guys, just started learning Lua (thanks to Corona!) this weekend and I am getting pretty close to grokking it.

I have one question which is probably best asked in code.
See "QUESTION" below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
local MakeShaun = function(passedIn)
 
        local _privateVar = -99
 
        local _iAmPrivateToo = {}       
        _iAmPrivateToo.p1 = "p1"
        _iAmPrivateToo["p2"] = "p2"
        _iAmPrivateToo[1] = "one"
        _iAmPrivateToo[2] = "two"
        _iAmPrivateToo.tbl = {x = 1, y = 2}
        
        -- private can access stuff in this closure
        local privateFunction = function() 
                return "privateFunction::_privateVar = ".._privateVar.." and passedIn = "..passedIn
        end
        
        -- public interface 
        return {
                
                -- I can't figure out how to access this from a 
                -- function inside the same table
                publicVar = 2,
                
                publicFunction = function()
                                
                        print("_privateVar = ".._privateVar)                            --> -99
                        print("passedIn = "..passedIn)                                          --> "hello" 
                        print("_iAmPrivateToo[2] = ".. _iAmPrivateToo[2])       --> "two"
                        print("_iAmPrivateToo.tbl.y = ".. _iAmPrivateToo.tbl.y)         --> 2
                        
                        print(privateFunction()) --> "privateFunction::_privateVar = -99 and passedIn = hello"          
                        
                        -- QUESTION: How can I access "publicVar" from a function in this table?
                        print("publicVar = "..tostring(publicVar))      --> Always prints "nil"  why?
                        
                end
                
        }
        
end
 
local myShaun = MakeShaun("hello");
 
myShaun:publicFunction()                                                                
print("myShaun.publicVar = "..myShaun.publicVar)                --> 2
myShaun.publicVar = "Carlos"                                    
print("myShaun.publicVar = "..myShaun.publicVar)                --> Carlos

Hmmm, I don't think you will be able to do it that way. I think your problem is that publicVar and your function are both elements (and peers) in your table. When you define publicVar=2, this is simply creating a new entry in your table with the key 'publicVar' and assigning it the value 2. This does not imply anything about scoping or visibility when you create your function. If you needed to access this variable from your function, you would need to access it through the table, e.g. myTable.publicVar, but since the table is anonymous and you are in the middle of creating it, you can't do this.

The simplest answer is that you need a variable declared before you start creating the table.

views:1465 update:2011/10/6 9:28:12
corona forums © 2003-2011