Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Returning type of display object
Started by Quantumwave Aug 09 2013 02:50 PM

13 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Quantumwave

[GLOBAL: userInfoPane.html]
Quantumwave
  • Contributor

  • 103 posts
  • Corona SDK

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

 

Thanks,

Dave

 



[TOPIC: post.html]
#2

jstrahan

[GLOBAL: userInfoPane.html]
jstrahan
  • Corona Geek

  • 1,926 posts
  • Corona SDK

[TOPIC: post.html]
#3

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

jstrahan

[GLOBAL: userInfoPane.html]
jstrahan
  • Corona Geek

  • 1,926 posts
  • Corona SDK

i was just thinking about that brent



[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,207 posts
  • Enterprise

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)



[TOPIC: post.html]
#6

Quantumwave

[GLOBAL: userInfoPane.html]
Quantumwave
  • Contributor

  • 103 posts
  • Corona SDK

Brent: That would work too.

 

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



[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,207 posts
  • Enterprise

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.



[TOPIC: post.html]
#8

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

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/

[TOPIC: post.html]
#9

Quantumwave

[GLOBAL: userInfoPane.html]
Quantumwave
  • Contributor

  • 103 posts
  • Corona SDK

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



[TOPIC: post.html]
#10

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,207 posts
  • Enterprise

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.



[TOPIC: post.html]
#11

Quantumwave

[GLOBAL: userInfoPane.html]
Quantumwave
  • Contributor

  • 103 posts
  • Corona SDK

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



[TOPIC: post.html]
#12

admin

[GLOBAL: userInfoPane.html]
admin
  • Administrator

  • 68 posts
  • Administrators

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.



[TOPIC: post.html]
#13

Tom

[GLOBAL: userInfoPane.html]
Tom
  • Moderator

  • 1,480 posts
  • Corona Staff

In the past, I've added the following code after the constructor: object.type = "group"



[TOPIC: post.html]
#14

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]