Two questions about classes and types

  1. Is there a way to get the name of a class object, and to get the name of the class that an instance belongs to? The Codea “class” function is discussed here: https://bitbucket.org/TwoLivesLeft/core/wiki/CodeaClasses , and it looks like the name is saved at a location called _base, but it doesn’t seem to exist, although I can grab hold of .is_a at the same location. My conclusion is that this bit of code isn’t the real implementation, or at least not its final version.

  2. Someone somewhere created this function (sorry, I didn’t bookmark it):

function typeOf(x)
    if x==nil then return 'nil' end
    if type(x) == 'table' and x.is_a then return('class') end
    local txt 
    if typeTable==nil then
        typeTable = {
        [getmetatable(vec2()).__index ] = 'vec2', 
        [getmetatable(vec3()).__index ] = 'vec3',
        [getmetatable(color()).__index ] = 'color', 
        [getmetatable(image(1,1)).__index ] = 'image', 
        [getmetatable(matrix()).__index] = 'matrix', 
        [getmetatable(mesh()).__index ] = 'mesh' ,
        }
    end
    local i = getmetatable(x)
    if i then txt = typeTable[i.__index] end 
    if txt then return txt end
    txt = type(x)
    return txt
end

It’s very handy. Does anyone know how to extend it to include the newest Codea types that aren’t listed in the function? Physics isn’t there, are there others?

Thanks.

@starblue You certainly can, I posted an approach to this a little while ago, if you have any questions just ask :slight_smile: let me know how you get on i hope it helps!

http://twolivesleft.com/Codea/Talk/discussion/3061/extending-type-for-class-name-reflection-and-other-helpful-class-reflection-extensions

Edit: It also covers almost all of the newest Codea types and has some more advanced functionality such as resolving base classes and entire class hierarchies :slight_smile:

@XanDDemoX. Okay, this reveals my beginner’s status with Lua and Codea. I know about _G but not what everything is that’s in it, exactly, nor exactly what metatables are. For example, how do you know that _G is invertible? I’m still reading the rest, but it is quite interesting.

@starblue i think i Am the one who posted this function :slight_smile: . Or maybe i just use it.

_G contains all your globals for the entire application so every function that you can access from anywhere is in there (i.e not local) and any classes you create’s meta tables will be in there, all of the lua functions and all Codea functions and classes are in there take a look the list is massive :slight_smile: (and will differ depending on the code you’ve written). However userdata (mesh for example) is not so straitforward.

for k,v in pairs(_G) do

print(k," ",v)

end

Meta tables are a little tricky to get your head round. A meta-table for a table describes how that table should behave and allows you to modify it, you can find more info here http://www.lua.org/pil/13.html.

Any tables keys and values are invertable :slight_smile: what I mean is I’m just swapping them round.
So every key becomes a value and every value becomes a key.

For example

local tbl = { cat = 1, dog = 2} -- key value lookup

local itbl ={1 = cat, 2 = dog} -- inverted keys and values

So knowing that _G contains meta tables organised by their string names is great because when you know the name of something you can use it.

Luckily class instances all have meta tables and they all share the same one for their class ( the one in _G unless you change it) which we can get a reference to with getmetatable.

However when you don’t know the name of something but you know how its been used (e.g a class instance who’s type you need to find out). _G in its normal form is not so helpful in this situation because there’s a good chance the instance will be local so not contained in _G, but if it was your not interested in the variable name anyway and other than that it contains named functions and meta tables.

So I make is a copy of _G which allows me to look names up by value. Which then enables you to find the type of something like this:


local _iG ={}

for k,v in pairs(_G) do

_iG[v]=k

end

local unknown = SomeClass() -- class instance

print(_iG[getMetaTable(unknown)]) -- prints SomeClass, (_iG is the inverted version of _G)