Returning type of display object

I’ve always wondered why the type() function doesn’t return “image” or “group” for such display objects. Can this be added?

Thanks,

Dave

+1

Hmmm… since “type()” is the Lua function for detecting the variable type (table, string, number, etc.), I’d rather not confuse this with Corona’s display objects. If we add this, I think something like “.objectType” would be more appropriate…

Brent

i was just thinking about that brent

Can’t we already figure that out?  Doesn’t the group table contain some other table that is it’s collection of members?  If that table is present its a group, if it’s not it’s an image (I think.  Perhaps there are other things I’m not thinking about)

Brent: That would work too.

Rob: Yes, it’s possible but is messy and possibly slower than a native function.

if type(object.insert) == “function” then is pretty efficient.  Invoking a function to execute those same calls would be more expensive.   Now an attribute, would be a little more efficient than having to call the type() function.

It’s quite easy to write a library extension to add a property which tells you the particular object use. If CoronaLabs were to make it a default, however, my preference would be .class as that’s what I use in my code.

http://www.coronalabs.com/blog/2013/07/02/tutorial-extending-libraries-without-native-code/

Rob, yes, having an attribute is definitely a better choice than user code (type(object.insert) == “function”) as any object with the ‘insert’ method returns true.

My request is to have one native method (or attribute) to determine the type of an object, instead of letting each developer write their own code that is either messy or can easily break if a table structure or method name is changed in the future.

Cheers,

Dave

Dave, I see your point, but consider the price.  I’ve been on the forums for a couple of years and this is the first request I’ve seen for it.  So lets say this would benefit 10% of the community (a liberal amount). It would mean writing an extra library routine that everyone would have to include even though 90+% would never use it.  We have to strike a balance against what features are included in the core vs. binary size of the output app.  We’ve had more complaints that 1.8mb was too big for the binary than we’ve had for functions like this. 

@horacebury’s blog post is probably the best compromise.  You an either extend the existing display objects to include an attribute or add your own function to implement a method (though if you’re in to efficiency) you’re probably going to use it in an if statement anyway, so I would argue my use of if type() is probably the most efficient CPU wise).

But of course, feel free to go to http://feedback.coronalabs.com and request the feature.  It’s how we process new features.

That’s fine Rob, let’s move on to more important features and bug fixes.

One clarification about the purpose of “type()”, as Lua is different from ActionScript 3.

In Lua, the type() method is designed to tell you what type a variable is (nil, string, number, function, boolean, table, userdata), but not it’s “class” type, which is what is meant by the type of display object.

In the past, I’ve added the following code after the constructor: object.type = “group”

Funnily enough, I posted a code snippet on the exchange which could be easily modified to add the name of the constructing function to any display.* library object:

http://developer.coronalabs.com/code/convenient-object-removal

The thought occurs that this could either be used to add a .type property or the type() function could be entirely overridden to return two strings: the existing return value and a new one to determine the object’s constructor.

+1

Hmmm… since “type()” is the Lua function for detecting the variable type (table, string, number, etc.), I’d rather not confuse this with Corona’s display objects. If we add this, I think something like “.objectType” would be more appropriate…

Brent

i was just thinking about that brent

Can’t we already figure that out?  Doesn’t the group table contain some other table that is it’s collection of members?  If that table is present its a group, if it’s not it’s an image (I think.  Perhaps there are other things I’m not thinking about)

Brent: That would work too.

Rob: Yes, it’s possible but is messy and possibly slower than a native function.

if type(object.insert) == “function” then is pretty efficient.  Invoking a function to execute those same calls would be more expensive.   Now an attribute, would be a little more efficient than having to call the type() function.