0

I wrote a simple library in which the user extends one of my abstract classes and then passes that class to one of my functions.

//user class
class My_robot extends Robot{
}

//My library function
function Robot_maker("path.to.My_robot")

The robot maker function then creates an array of the type Robot[]. The user then can have the array returned to them by calling another one of my functions Robot[] get_robot_army()

I know that I can cast the Robot[] array element by element but is there a way to cast the entire array at once? (I want to abstract this away from the end user)

Is there a way to use Reflection to change the return type of a function?

///////////////////////////// There is some need for clarity ///////////////////////////
The code in question is the function batch_result() on line 124
I want to be able to return the class that the user supplied using the function batch_set_relationship() on line 139
As an example if some one ran here on line 88 how can I pass the user an array of the supplied custom supplied class.

Also can some one clarify why the cast to an array of a different type fails.

7
  • 4
    This does not appear to be Java. (function?) Commented Dec 16, 2010 at 15:57
  • 4
    This is your 27th question on StackOverflow, over the course of eight months. With respect, it's well past time that you learned how to properly format code so others don't have to clean it up for you. Commented Dec 16, 2010 at 15:57
  • > I know that I can cast the Robot[] array element by element but is there a way to cast the entire array at once? To what, from what, and why? Commented Dec 16, 2010 at 15:59
  • unclear what you're trying to do here. (also, syntax doesn't look very java-ish). So you have a method that returns an array of Robot elements. Where do you need to cast ? Here I would imagine you only want to expose the abstract Robot type (both for creating and retrieving). Commented Dec 16, 2010 at 16:02
  • I know I know I do talk in spaghetti. I also can not wright a lick of JS. Commented Dec 16, 2010 at 16:16

2 Answers 2

1

Responding to the comments above:

I would just return the Robot[] array and cast each element of that array.

As I said before, cast to what? And why? The return type of the user's function should be Robot[]. The user sublasses Robot to do what they need to do, and returns Robot[] containing instances of their subclass. Your code doesn't need to know what the subclass is; that's the whole point of polymorphism.

Sign up to request clarification or add additional context in comments.

Comments

0

One way you can think, add the complete array into an list or any collection and send that as a return type

In that way you can achieve the casting of return type.

With the reflection class you cant change the class type but you can defnitely get the class type dynamically, then later you need to modofy the class type to meet your requirements

1 Comment

I understand what your saying. I agree. The then later part was what I was trying to come up with crazy ways to hide from the end user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.