I've always wondered why the type() function doesn't return "image" or "group" for such display objects. Can this be added?
Jump to content
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.
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.
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.
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:
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.
Community Forum Software by IP.Board