type checking?

I am looking for a kind of type checking in codea. There is no assert in codea so the lua approach
doesn’t work
http://lua-users.org/wiki/LuaTypeChecking

Edit: Found the type() command …

assert() function exist in codea and i think type() exist too, but type() is just for number, table and function, if you want try to know the class, the function is is_a(ClassName).

When testing a class, I do:

if type(a) == "table" and a.is_a and a:is_a(ClassName) then

Since lua bails out on the first false test, it doesn’t abort the program just because I fed it a number or a string or a generic table by mistake. (I suppose I should put and type(a.is_a) == "function") in there as well for absolute surety.)

@HyroVitalyProtag: I searched wiki an didn’t find the assert() command. https://bitbucket.org/TwoLivesLeft/core/wiki/Documentation

@Andrew_Stacey: Thank you.

There is a problem when you work with codea special types (vec2, etc…): the type is always userdata. So i made this function typeOf() to enlarge type():

function typeOf(x)
    if x==nil then return "nil" 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

i have picked that from another program and removed some of my stuff but not checked it, so there might be a minor syntax error in the code above.

A lot of functions are not in documentation :wink: But majority of functions you can find at lua-users.org exist in codea.

Yes I noticed that. I would be nice to know which don’t exit in codea. Just a list of the commands in codea would be nice - no documentation - just a plain list.

if you would the list of defined function, you can use ‘getfenv()’ or _G and make a recursive print :

local tab = getfenv()
function printRecursive(tab)
if (type(tab) == "table") printRecursive(tab)
else print(tab)
end

or something like this (I haven’t my iPad on me for test if this work)

Thank you - I will try that at home.

@Hydo: I patched something together what seems to work:


function allcommands()
        for n,v in pairs(_G) do
            print(n,v)
        end
end

@Jmv38 - I added Andrew’s class check in, and it all works well

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