lua-users home
lua-l archive

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


Mark Hamburg wrote:
> What's probably needed here is a __type metatable entry and a rawtype
> function which ignores it. Any individual user could certainly add it
> and replace the existing definition of type such that it obeys it,
> but agreed standards would be good here.   
> 
> (Or if one doesn't want to replace the existing routine, we need
> extendedtype and type.) 

In my project, I have such a __type in my metatables, and I replaced the
default 'type' function with a function which returns multiple values.
If __type is a table it returns that table unpacked. If it's a function
it calls it and returns all its return values, and if it's a single
value it returns it as second return value, with the traditionnal type
as first return value. In the case of the table and function, it's the
responsability of the type creator to decide if he wants to return the
traditionnal Lua type as first value or not. Example:

local object = mylib.new()
local type,subtype = type(object)
if type=='userdata' and subtype=='image' then
    display(object)
end

I can share my implementation of 'type' is someone is interested.