[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Forward declaration of a table and (closure) function
- From: Ross Berteig <Ross@...>
- Date: Wed, 13 Jul 2011 18:34:28 -0700
At 06:21 PM 7/13/2011, 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})
You want something like this:
require "spreadsheet"
local columns = spreadsheet.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})
Note the use of colon notation for the calls to newCol() but not
to the call to new(). Requiring the file begin with a call to
require() is not unreasonable. However, if this file is really
intended to be edited by a user as if it were a configuration
file your system could insert that call ahead of parsing the
user's file.
>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
That can't work because you are actually calling the value
stored in Columns.new before you've stored a function there. The
forward declaration of a Columns variable doesn't permit you to
index a field of the table it doesn't hold yet, after all.
>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?
Using require() has the advantage that you separate out the
mechanism and isolate it from the parts the user is expected to
modify.
Ross Berteig Ross@CheshireEng.com
Cheshire Engineering Corp. http://www.CheshireEng.com/