Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Does this feature exist? (returning downcasted object by method's argument or diamond interface)  RSS feed

 
Jon Crow
Greenhorn
Posts: 8
1
Firefox Browser Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there is this nifty feature in Unity3D's C# Component scripting.

Unity (Game engine) has gameobjects that can have custom Components (Objects that extend Component) on them to do various things like rendering and scripting.
You can access another component of type from your own component script by using the following:


gameObject is a variable on every Component (it is the object the component is attached to).

I don't know much about diamond operators, but in my own engine's code, I have to do the following currently:


--

As you can see, this means I have to cast it back to my desired class every time, whereas in Unity's C# it automagically does this by returning based on diamond operator (mentioned before).

While the current thing I have set up in my engine works just dandy, I would like to make it smaller (while keeping the meaningful naming of methods and such) because I plan on other people using my API.
I really just want to return a specific downcasted type (given by argument), and I don't really know if this is possible.

Even if I can do:
SpriteRenderer spr = gameObject.GetComponent(SpriteRenderer.class);
It would be great.. So does this exist in Java? I would google it, but I've already done that, and I honestly have NO idea what this type of thing is even called..
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon,
Good question. Have a cow.

In Java, generics (the diamond operator) was retrofitted on and only exists at compile time. So it can't do magic casts.

In your example, the GetComponent() method returns a Component so needs the cast. If you wanted to return the type form a generic, it would look like this:



However, that won't help here as you have a List of Component objects. So I'm going to say the feature you want doesn't exist.

Note: Method names begin with lowercase in Java, so it would be getComponent().
 
Jon Crow
Greenhorn
Posts: 8
1
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, that clears things up. I suppose I'll just cast it every time, not a huge downer.
Thanks so much Jeanne!
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my response here: https://coderanch.com/t/679543/java/interfaces-inherit#3187403

Basically, you might be able to use a Predicate and methods in Class like isInstance(Object) and isAssignableFrom(Class) to do something similar in Java. It's still not something that's built in to the language but at least it seems like it would be a reasonable alternative to some really messy designs that you might come up with without these.
 
Rob Spoor
Sheriff
Posts: 21047
85
Chrome Eclipse IDE Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you want is possible, and not so hard, thanks to two methods: Class.isInstance and Class.cast:
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice, Rob. A cow for your suggestion.
 
Rob Spoor
Sheriff
Posts: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know about cast(). Cool!
 
Jon Crow
Greenhorn
Posts: 8
1
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woah, this is sweet. Thanks so much everyone!
Exactly what I needed. Is there a way to mark topic as solved?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there's a small icon at the bottom of the page where you can "suggest" it as solved and then a moderator can click the solved button. I've done the later so it is now marked as solved.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!