[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Strange userdata metatable behavior in C API
- From: Sam Roberts <sroberts@...>
- Date: Mon, 4 Jun 2007 10:10:13 -0700
On Mon, Jun 04, 2007 at 12:55:01PM -0300, Roberto Ierusalimschy wrote:
> > Each time I have begun a project using lightuserdata, I eventually
> > switched to full userdata. I found the automatic management of an
> > object's lifetime outweighed the small time+space penalty for creation
> > of a full userdata. I suppose we cannot remove lightuserdata completely. :)
>
> Light userdata was introduced in the language with the only purpose
> of allowing programs to map C pointers back to their respective full
> userdata, using light userdata as keys in a table. (Until Lua 4.0
> the API did that automatically.) It was not intended to be a "cheap"
> replacement for full userdata.
You make this very clear in the PIL.
Unfortunately, the type() of both are "userdata", and the description as
being userdata, but lighter, leads people astray fairly regularly.
Its probably unwanted API tweaking, but some other name ("externaldata",
"pointer", ...) would have made it more clear.
Btw, has there been consideration to having a "__typename" metamethod?
I.e., redefine type() as something like:
do -- untested code
local _type = type
function type(o)
local mt = getmetatable(o)
if mt then
local tn = mt.__typename
if tn then
if type(tn) == "string" then
return tn
else
return tn(o)
end
end
end
return _type(o)
end
end
I guess that you can do it yourself, like the __pairs metamethod, is
enough reason to exclude it from the core language.
Cheers,
Sam