lua-users home
lua-l archive

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


FWIW, the OOP type of a table can be easily obtained, but not in O(1).
More elaborate variations are possible (e.g. look in package.* as well).

function ootype(o)
    local mt = getmetatable(o)
    if not mt then return nil end

    for k, v in pairs(_G) do
        if v==mt then
            return k
        end
    end
end

-- example

A = {}

function A:new(o)
    self.__index = self
    return setmetatable(o or {}, self)
end

a = A:new()
print(ootype(a))

On Sat, Jun 28, 2008 at 2:28 AM, ketmar <ketmar@ic.km.ua> wrote:
On Sat, 28 Jun 2008 01:19:13 +0100
Stephen Kellett <lua@objmedia.demon.co.uk> wrote:

> There are a lot of built in types in JS. Don't beleive me? - please
> examine the Firefox source.
can't find anything there. number, string, boolean, array, object, null,
undefined. that's all. 'native objects' aren't 'built-in types'.

just looked at my own Lua sources: all my classes has "tostring" and
"className". it's not enforced by the API (and it's good), but it's
there, i'm not too lazy to write several lines of code.

either i don't understand you, or you don't understand me. i can't see
any reason to enforce type info in userdata registration (as done for
'native objects' in JS). it's a price of freedom. API with restrictions
or self-restrictions. JS chooses first. Lua chooses second. i
personally prefer Lua way.

and btw: spidermonkey API is sooooo ugly... %-(