Linked by Bjorn Raupach on Thu 17th Jul 2008 06:01 UTC
Thread beginning with comment 323478
To view parent comment, click here.
To read all comments associated with this story, please click here.
To view parent comment, click here.
To read all comments associated with this story, please click here.
RE[3]: Never programmed in Java before...
by saucerful on Thu 17th Jul 2008 18:29
in reply to "RE[2]: Never programmed in Java before..."
Java doesn't have data structs so it would have to be a class but the same concept works.
In fact it makes sense... if you're returning multiple values, they have a relationship. Chances are that that relationship will be studied elsewhere in the code. For instance other methods will take as input both of those pieces of data. Or there will be methods that specifically massage that data. What better place to put them than in this return value class? So in the end such a class almost always pays off by making code easier to read and more organized.
Edited 2008-07-17 18:31 UTC
RE[3]: Never programmed in Java before...
by FunkyELF on Thu 17th Jul 2008 18:43
in reply to "RE[2]: Never programmed in Java before..."
My first thought in C++ would be to have a templated ReturnTriple struct along the lines of:
template
struct ReturnTriple
{
A first;
B second;
C third;
};
which is cheap, type-safe and reusable.
template
struct ReturnTriple
{
A first;
B second;
C third;
};
which is cheap, type-safe and reusable.
The first poster on here gave a nice link for javatuple which does something very similar. I just looked and it seems real nice. it has classes for Single, Pair, Triple etc. up to Decuple.
I'm not sure if your C++ example retains type safety but this javatuple package does.
EDIT: Or, as another poster pointed out, just pass parameters by reference. Is this not possible in Java?
Java has Objects and primitives (because back in the day people were bitching about performance). Primitives are pass by value, Objects are passed as a reference. This means that if you pass a Vector to a method, because it is an Object, if you add or remove from that Vector in the method the changes will be visible after the method is called. However, even though Integer is a class, there are no methods to change the value of one. So, a method that needs to return two integers could return a List<Integer> or an Integer[], but things get ugly when you need to return an Integer and a String.




Member since:
2006-02-01
1.) Java doesn't have pointers.
2.) There's no type safety in that approach (meaning it's not a good idea for C++ too) "
My first thought in C++ would be to have a templated ReturnTriple struct along the lines of:
template<class A, class B, class C>
struct ReturnTriple
{
A first;
B second;
C third;
};
which is cheap, type-safe and reusable.
EDIT: Or, as another poster pointed out, just pass parameters by reference. Is this not possible in Java?
Edited 2008-07-17 17:37 UTC