lua-users home
lua-l archive

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


U D Man Lorenzo!!!

See below...

On Thursday, July 14, 2011 05:16:34 AM Lorenzo Donati wrote:
> On 14/07/2011 3.21, Steve Litt wrote:

> > 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()
> ----------------------------------------------------------------

I ran your preceding code and it worked, so you obviously had it 
right. All I needed to do was understand it.

I don't know metatables so at first I thought you did something 
magical, especially since you used the word "closure()" as a function. 
First thing I did was change both instances of that function name to 
make sure it wasn't a reserved word deal, but it worked with the other 
name. Then I took a very close look at your closure(), and realized it 
was just a subroutine to postpone the running (as opposed to 
declaring) of the columns.newCol() calls until after everything had 
been defined.

Turned out for some reason I couldn't get columns=Columns.new({}) to 
work inside of the closure when I converted everything to my way of 
making objects, but that was trivial to solve. Watch this:

===============================
#!/usr/bin/lua

function user_configuration(columns)
	
	-- ==== BEGINNING of USER-CUSTOMIZABLE OPTIONS ====
	-- 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})
	-- ==== END of USER-CUSTOMIZABLE OPTIONS ====
	
	return(columns)
end

local Columns = {}       -- THE CLASS
function Columns.new(property_names_a)
	self={}
	--local vars to keep state
	--functions to act as methods
	function self.newCol(name, properties)
		--set up all the stuff about this column
	end
	return self
end

function this(a) return "this" end
function that(a) return "that" end

local columns = user_configuration(Columns.new({}))

--AT THIS POINT, columns HAS EXACTLY WHAT IT SHOULD
===============================

It worked perfectly. You actually solved a second problem for me -- I 
needed to associate a formatting function with each column, and they 
needed to be defined before they could be used by Columns.newCol(). No 
problem, I did that stuff AFTER defining the formatting functions.

Clean as a whistle -- I'm going to use this every time there's user 
config too complex to put in a text file. This is pure genius. Thank 
you!

I'm also, in the next few days, going to use your OOP techniques as a 
starting point to study OOP with metatables, which I understand is the 
only way to do inheritance.

Thanks so much!

StevET

Steve Litt
Author: The Key to Everyday Excellence
http://www.troubleshooters.com/bookstore/key_excellence.htm
Twitter: http://www.twitter.com/stevelitt