lua-users home
lua-l archive

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


>From: erik@hougaard.com (Erik Hougaard)
>
>A userdata type with field addressing:
>
>Example 1.
>Customer.Name = "Erik Hougaard"
>Customer is a userdata, Name is "just" a field in my userdata.

you can do this using the "get/settable" tag methods.
one way is to keep a global table indexed by userdata.
the value corresponding to an index would be a table of fields associated
with the userdatum.
something like (untested code ahead):

do
 local T={}
 settagmethod(tag(u),"settable",
	function (t,x,y)
		local a=%T[t]
		if a==nil then a={} %T[t]=a end
		a[x]=y
	end)
 gettagmethod(tag(u),"gettable",
	function (t,x)
		local a=%T[t]
		if a==nil then return nil else return a[x] end
	end)
end

note the use of T as upvalues for the tag methods.
the table T is created, remains alive, but is not acessible by name outside
the do ... end. only the tag methods know about it.

>1. To a tag there should be option to add a "read" and a "write" function

"read" and "write" are library functions, not part of the language.
nevertheless, in 3.2, you'll be able to write your own "tostring" function and
"write" and "print" will use it.

>2. Userdata should be allowed in expressions ??

it is. but most of the time you'll get tag methods called.

>3. Lua should keep the "fieldname" for passing.

I don't follow you here.
I think the scheme above could do what you want.
This scheme was part of the original design of userdata and tag methods:
to allow data strucutres to be hybrid: a userdata would represent some
struct in C and also a table in Lua and fields in both would be accessible
in a transparent way.
(to do this, the scheme above would have to be augmented to look at the
field names and call C for some of those.)
--lhf