To read all comments associated with this story, please click here.
Oh, please.
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
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:
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.
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).






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.