In python you can do this:
def f():
return 1, 2, 3
(foo, bar, baz) = f()
Is there an equivalent in java?
In python you can do this:
def f():
return 1, 2, 3
(foo, bar, baz) = f()
Is there an equivalent in java?
tl;dr: No, there isn't such a thing in Java.
You can assign initial values to variables like this:
int foo = 1, bar = 2;
But if your want (1, 2, 3)
to be the result of a method call, this is not possible in Java. Java does not allow returning multiple values.
Python allows this:
def foo():
return 1, 2, 3
a, b, c = foo()
The main point, why this does not work in Java is, that the left hand side (LHS) of the assignment must be one variable:
Wrapper wrapper = WrapperGenrator.generateWrapper();
You can not assign to a tuple on the LHS as you can in Python.
If you want (1,2,3) to be the result of a method call, you can use an array:
int[] arr = {1,2,3};
int[]
? –
Stoops I realize this is an old post, but I could point out a way that comes somewhat close to this.
Using the OP's example (This could obviously use multiple types instead of three ints, but I'm going with the original example),
class Trip{ int foo;int bar;int baz;} // After building this class, have your editor create a constructor for you!
public Trip() {
return new Trip(1,2,3);
}
Trip t = f()
Now at this point I realize you haven't actually ASSIGNED this to three local variables (or member variables) as you would have liked, however you do have a local variable you can use as though you had... like:
t.foo * t.bar + t.baz;
The advantages here are that this is quite terse, hardly more work than the python example and safer/clearer/easier to read; as is I'd be really happy with this!
Perhaps more important though is that as you go on you will realize that this mini-class should have been a real class all along since the values are going to be somewhat associated since they are from the same function!
You can change what you have to a real class with simple editor refactors--replace member access with getter/setter, make members private, extract the class into it's own file and you have a full class!
Chances are that you'll eventually figure out that your original function f() should have been a member (or constructor) of this class in the first place!
By the way, the disadvantage is that this will fully trigger some Java programmers that don't understand that rules were meant to be broken--if you have old coders who are insistent on stuff that seems pedantic (rule-obsessed) it's best not to try this because it'll make their head explode (all over you, most likely).
As I noted in this other question, if your goal is specifically to return multiple values from a function, in Java 16+ you can accomplish something similar to this using a record type:
record FooResult(int a, int b, int c) { }
FooResult produceFoo() {
return new FooResult(1, 2, 3);
}
/* ... */
var f = produceFoo();
System.out.println(f.a + "," + f.b + "," + f.c);
This will let you return any set of types, with named fields, at the expense of having to add a line to declare the record. This won't really help with assignments like (i, j) = (j, i)
however.
© 2022 - 2024 — McMap. All rights reserved.