-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
varargs ambiguity with generics #285
Comments
You are probably correct. We rely on a fork of commons-lang to find which method matches using |
I'm happy to work on this if that's helpful -- let me know. |
Absolutely that would be great. |
Here's my suggestion for the desired behavior: the javascript call If you want a single array arg to be passed to the java method, then you'd do Does this behavior make sense? |
Can we make the rules the same as Java? How does it handle these cases? |
Based on the little experiment below, I'd say the existing behavior is correct if we want to make things the same as Java. So this is not a bug, though we might want to add something to the README to explain this behavior. import java.util.Arrays;
import java.util.List;
public class testvarargs {
public static void varargs1(int... numbers) {
System.out.println(numbers[0]);
}
public static void varargs2(Object... numbers) {
System.out.println(numbers[0]);
}
public static <T> void varargs3(T... numbers) {
System.out.println(numbers[0]);
}
public static void main(String[] args) {
int[] ints = new int[] {1, 2, 3};
varargs1(ints); // prints 1
varargs2(ints); // prints [I@7852e922
varargs3(ints); // prints [I@7852e922
}
} |
I haven't looked at the varargs handling code, but I'm assuming it's because of the ambiguity due to the
asList
method being generic (List<T> asList(T... a)
) - there's no way to distinguish if the javascript Array being passed into Arrays.asList() is intended as a single value of typeT
or if it's intended as aT[]
.Assuming that's the issue, we could detect this ambiguity and throw an error, requiring the javascript programmer to use apply when they want a javascript array to be 'spread' into a varargs generic function.
Does this make sense?
(Btw, ES6 compiled by babel works just fine here with
Arrays.asList(...arr);
).The text was updated successfully, but these errors were encountered: