lua-users home
lua-l archive

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


Hello,

I am assuming that my original message didn't make a whole lot of
sense.  I think it is a reasonable question, so I will try to explain
(if anything, explain the only workaround that I could find).  I
would appreciate any comments.

Assuming that getDimension() returns a userdata object with tag methods
that provide for accessing of the x, y, dx, and dy fields of a C struct 
defined like so:

	struct Dimension { unsigned int x, y, dx, dy };

Perhaps this corresponds to the dimensions of the top GUI window.

Also, assume that a setglobal() tag method is defined so that
assigning a table to a variable containing a Dimension value actually
updates the fields of the current userdata object (the implementation
actually ignores the variable name; instead making the changes to
the "old" value).  This would allow the following:

d = getDimension()
d = { x = 10, y = 10, dx = 100, dy = 200 }

Essentially, this makes the variables typed and not just their values.
I wish to provide this syntax so that one does not need to reference 
the variable.field for each assignment.  Essentially, this is like
providing the following function:

function Update(varname, table)
	local tmp = getglobal(varname)
	local i,v = next(table, nil)
	while i do
		tmp[i] = v
		i,v = next(table, i)
	end
end
Update("d", { x = 10, y = 10, dx = 100, dy = 200 }

I don't find this as readable.

Anyhow... This all works fine and dandy as long as the variable in 
question is a global.  However, something like the following in a 
function won't:

function setSize(X, Y)
	-- ... do some stuff
	local d = getDimension()
	d = { x = X, y = Y }
	-- .. do some stuff
end

The problem is that the setglobal() tag method is not used.  The only
workaround that I have found is something like this:

function setSize(X, Y)
	-- ... do some stuff
	local d = getDimension()
	local set = gettagmethod(tag(d), "setglobal")

	set("ignored", d, { x = X, y = Y })
	-- ... do some stuff
end

Anyhow, that is my solution.  It is still not as clean as I would
like.  It would be nice if there were set/getlocal tag methods,
or if locals could be accessed via an implicit local table (can
they?).

thanks,

ajk