lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 14/07/2011 3.21, Steve Litt wrote:
Hi all,

In a program I'm doing "OOP" using a table called Columns and a
function called Columns.new() containing several other functions that
become, for want of a better word, "methods".

The purpose of this construct is as use-modifiable setup, so I'd like
to have the calls to it at the very top of the program, where users
expect user-modifiable stuff to be. So what I'd like at the top would be
something like:

columns = Columns.new({})
-- THESE *MUST* BE IN SPREADSHEET COLUMN ORDER!!!
-- IF THE SPREADSHEET CHANGES, CHANGE THIS LIST
columns.newCol("recvdate"  , {quantfield=false, fcn=this})
columns.newCol("fname"     , {quantfield=false,  fcn=this})
columns.newCol("lname"     , {quantfield=false,  fcn=this})

Trouble is, these bomb if Columns and Columns.new() are defined below
them. I can do this above them:

local Columns

and then define Columns below, but if I do this:

local Columns, Columns.new

then it bombs with:
slitt@mydesk:/d/at/lua/massmail$ ./colnames.lua
/usr/bin/lua: ./colnames.lua:3: unexpected symbol near '.'
slitt@mydesk:/d/at/lua/massmail$

Is there a syntax to forward declare both Columns and Columns.new(),
or will I have to byte the bullet and make those definitions in a
separate file that I must require?


Would this be acceptable (almost untested code)?


----------------------------------------------------------------
local Columns
local closure = function()
   -- ========= BEGIN of USER-CUSTOMIZABLE OPTIONS =========
   columns = Columns.new({})
   -- THESE *MUST* BE IN SPREADSHEET COLUMN ORDER!!!
   -- IF THE SPREADSHEET CHANGES, CHANGE THIS LIST
   -- *** NOTE: dots changed to colons
   columns:newCol("recvdate"  , {quantfield=false, fcn=this})
   columns:newCol("fname"     , {quantfield=false,  fcn=this})
   columns:newCol("lname"     , {quantfield=false,  fcn=this})
   -- ========= END of USER-CUSTOMIZABLE OPTIONS =========
end


Columns = {}	-- remember, it's local
Columns.__index = Columns
function Columns.new( t )
   t._data = {}
   return setmetatable( t, Columns )
end

-- stub method
function Columns:newCol( name, data )
   self._data[ name ] = data
   print( "Adding a column '" .. name .."' to "
      .. tostring( self ) .." with data " .. tostring( data ) )
end

closure()
----------------------------------------------------------------

Output:

Adding a column 'recvdate' to table: 00463EE0 with data table: 00464CA0
Adding a column 'fname' to table: 00463EE0 with data table: 00464E10
Adding a column 'lname' to table: 00463EE0 with data table: 00464F50




Thanks

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt





-- Lorenzo