Linked by Bjorn Raupach on Thu 17th Jul 2008 06:01 UTC
Java Today was one of those days when I wished Java would support multiple return values. I had to develop a rather CPU-intensive algorithm which would compute a solution for a knotty constraint problem. Having a solution alone is sometimes not enough and you also need to add some parameters which measure the quality of the computed outcome. Most of these accompanying parameters can or have to be computed within the algorithm itself, but Java allows you to return only one value either an object or a primitive type. People working with Lisp, MATLAB or Perl, just to mention a few, don't have a problem like this at all. Functions supporting multiple return values is already implemented at the language level and frameworks make heavy use of this. But as a Java programmer you are pretty much stuck here and need to consider some other means to come out of this situation. In the following I would like to give some hints on that topic. Hopefully they are of help for anyone having the same problem every now and then.
Thread beginning with comment 323573
To read all comments associated with this story, please click here.
I guess you never tried Python
by Glynser on Fri 18th Jul 2008 09:31 UTC
Glynser
Member since:
2007-11-29

All those who claim that it's "useless" have never tried Python. If Java would borrow Python's multiple return types, it would look like this:


public (String, int) findName(List<whatever> names)
{
String name;
int number;

// do some searching inside the list...
// ...

// finally return the name and the amount of
// data that has been searched through (just an example)

return (name, number);
}


String name, int number = getName(someList);

System.out.println("Found: " + name);
System.out.println("I had to search through " + number + " items");


Now tell me that ain't useful? Of course it's not something you need everyday (and it might really be bad design in many cases), but there are cases where you want to return two things that are not so closely connected to each other that they really belong into the same class.

I have used it in Python before and it's very useful. I wish Java had this.

danieldk Member since:
2005-11-18

All those who claim that it's "useless" have never tried Python.


Oh, please. ;)

Now tell me that ain't useful? Of course it's not something you need everyday (and it might really be bad design in many cases), but there are cases where you want to return two things that are not so closely connected to each other that they really belong into the same class.


Sure you can do the same thing. Return an immutable list of 'Object's. Because that's what Python actually does (it returns a tuple). That's different than allowing for multiple return types (viewed from a static language).

People don't do this, because it sacrifices type safety. Of course, you will have to cast it back to some class type to make its methods visible again. (And you can't apply some of the syntactic cleverness that some dynamic languages offer in the calling code.)

If you don't want to do that, you are picking the wrong language. Java is a statically typed language, Python is dynamically typed. If you want a dynamic language for a particular job, use a dynamic language. Don't expect a static language to act as a dynamic or vise versa.

Edited 2008-07-18 14:20 UTC

Reply Parent Bookmark Score: 2

andyleung Member since:
2006-03-24

All those who claim that it's "useless" have never tried Python. If Java would borrow Python's multiple return types, it would look like this:


public (String, int) findName(List names)
{
String name;
int number;

// do some searching inside the list...
// ...

// finally return the name and the amount of
// data that has been searched through (just an example)

return (name, number);
}


String name, int number = getName(someList);

System.out.println("Found: " + name);
System.out.println("I had to search through " + number + " items");


Now tell me that ain't useful? Of course it's not something you need everyday (and it might really be bad design in many cases), but there are cases where you want to return two things that are not so closely connected to each other that they really belong into the same class.

I have used it in Python before and it's very useful. I wish Java had this.


First of all, I would like to give you a very basic exam on how to call a method:

String name, int number = getName(someList);


Where did you get that method from when your method defined before was findName(...)?

Besides, you really need to learn what is OO. Even this example is not enough to proof you have enough OO knowledge. Let's say your example is to show that customer name with total number searched. You create an object that wraps these two values and pass it back to the caller until you reach your "View" and show it. This is value object pattern and if you create Frontend application, this becomes, somehow in web sense, your "Model" in "MVC". Tell me, if you have 5 layers (caller methods) between this "findName()" method and the frontend. During development cycle your requirement is changed 3 times for return value to include "long searchedInMilliSeconds, int listSize", you have to change all 5 layers (may be more than 5 caller methods) because they all return multiple values. I don't think this makes any of sense.

In the end, if you like the way of returning multiple values, why do you want "List" as parameter? You simply extract all values from this list, pass them all as parameters like the following:

public (String, int) findName(String name1, String name2, String name3, String name4, String name5...[until all names are passed]){
...
}

If you really like this way of programming, then I guess you don't even need/like to use array. If this is the case you may want to find out where your strength is really in instead of programming.

Reply Parent Bookmark Score: 0

j-kidd Member since:
2005-07-06

You have never tried Python.

Reply Parent Bookmark Score: 1

Glynser Member since:
2007-11-29

Well, I am SOO sorry that I once wrote getName und once findName - this was an EXAMPLE, it's likely to make such a mistake if you're writing this stuff into that comment box here...


And don't tell me I don't know about OO. This actually hasn't got anything to do with OO, it's just a tiny, tiny EXAMPLE of why multiple return types CAN actually be useful, or - maybe better - how an actual implementation would look like in Java. Cases where it might actually be useful should be made up by everyone themselves, I think every programmer should have just about enough phantasy to do that.

And yes, it's not useful for EVERY case (remember the sentence: "When you've got a shiny new hammer, every problem looks like a nail"). But there ARE some cases where it is indeed useful.

Python implements that feature. What do you think is the justification for this - widely used - feature in this - widely used - language, if it ain't useful and hasn't got anything to do with OO and and and...

Also, it hasn't got anything to do with type safety (not sure if it was you who mentioned that or someone else), because as you can see in my example, it would be actually VERY typesafe indeed. Because the types "String" and "int" are actually written there. And if you would return some other stuff instead, a good compiler _might_ tell you about that. Yes, Python just returns tuples, but they can be immediately assigned to seperate values (JUST like in my Java example) and if it would be included into a statically typed language, who would claim that it would be necessary to use some kind of Tuple<Object, Object> internally? No one, because it would be bullshit. And I didn't say that either. It would be no problem for static types (JUST like in my Java example).

Reply Parent Bookmark Score: 1