Identifying variables etc in CodeaEdit2:

Hi All,

Just playing with a new project and decided to try to identify variables passed to functions by type. I know a few of the variable types and how to identify them but not sure about the full range of varibles etc open to Codea. Can anyone point me to any documentation for Codea or Lua ?

Edit: The reasons behind this are so that I can test for a variable type on passing to a function which then enables me to select the result option by calling the relevant subroutine in that function. Hopiing this would make my code more compact and flexible.

Edit2: Aaaah answered my own question, been coding in Lua for 12 years now and forgotten the basics. There are only 4 data types in Lua and they are identified in their application. Have to re-think my coding now.

In case it’s of use, here’s my extended is_a function.

--[[
The function "is_a" extends the capabilities of the method "is_a" which is automatically defined by Codea for classes.
 
Parameters:
a: object to be tested
b: test
 
The tests work as follows.
 
1. If the type of b is a string, it is taken as the name of a type to test a against.
2. If the type of b is a table, it is assumed to be a class to test if a is an instance thereof.
3. If the type of b is a userdata, the test is to see if a is the same type of object.
4. If b is a function, then it is replaced by the value of that function.
--]]

function is_a(a,b)
    if type(b) == "function" then
        b = b()
    end
    if type(b) == "table" and b.___type then
        b = b()
    end
    if type(b) == "string" then
        return type(a) == b
    end
    if type(b) == "table"
    and type(a) == "table"
    and a.is_a
    then
        return a:is_a(b)
    end
    if type(b) == "userdata"
    and type(a) == "userdata"
    then
        if a.___type or b.___type then
            return a.___type == b.___type
        end
        return  getmetatable(a) == getmetatable(b)
    end
    return false
end
2 Likes

@LoopSpace - thanks for the feedback, that’s given me something to think about and play with.

That’s very cool. Can you show how is_a is used in Codea, both with and without your changes?

Here’s an example from my VecExt code. This defines inline multiplication between a vec4 and various other types: number, matrix, and another vec4.

The is_a that comes with Codea is only for user-defined classes, it can’t handle native types (number, string, table) nor Codea’s extensions (vectors, color, etc). The purpose of my code is to enable comparison of type between just about everything.

m["__mul"] = function (a,b)
      if is_a(a,"number") then
	 return mul4(a,b)
      end
      if is_a(b,"number") then
	 return mul4(a,b)
      end
      if is_a(a,matrix) then
	 return a:__mul(b:tomatrixleft())
      end
      if is_a(b,matrix) then
	 return a:tomatrixleft():__mul(b)
      end
      return vec4(
	 a.x * b.x - a.y * b.y - a.z * b.z - a.w * b.w,
	 a.x * b.y + a.y * b.x + a.z * b.w - a.w * b.z,
	 a.x * b.z - a.y * b.w + a.z * b.x + a.w * b.y,
	 a.x * b.w + a.y * b.z - a.z * b.y + a.w * b.x
		 )
   end

…oh is this a Codea4 thing? I’m just getting an error from trying to use is_a in normal Codea.

@loopspace - thanks for the feedback and example. I’ve just been trying to pass a number of parameters to a function and wanted to check the first parameter for type as I was trying to pass either an image or a string to it and then produce the relevant output for them. I just wanted to check if it was possible as an alternative to passing two variables.

Edit: sorry about that @loopspace @ubergoober reading two threads at once, hence confusion. @dave1707 - ignore my previous mail. Thanks.

Did you mean dave1707 in that last comment, @Bri_G? They haven’t participated in this discussion as yet – that I can see – so that’s a bit confusing.