-
Notifications
You must be signed in to change notification settings - Fork 55
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
StreamUtil.interleaving() giving strange ClassCastException #43
Comments
The problem here is type erasure I think. If you wrap your lambda in a parameterized method (like it is done in Selectors.roundRobin()) I think it should work. I am not an expert but I have solved this kind of problem by using actual collections instead of arrays. I can put a patch to illustrate. |
|
This does it. I think the |
It's a (very peculiar) peculiarity of type erasure. I still don't quite understand why Selectors.roundRobin() works, while pulling the selector created in Selectors.roundRobin() inline like so: @Test
public void interleaving() {
Selector<String> alternating = new Selector<String>() {
private int startIndex = 0;
@Override
public Integer apply(String[] options) {
int result = startIndex;
while (options[result] == null) {
result = (result + 1) % options.length;
}
startIndex = (result + 1) % options.length;
return result;
}
};
List<String> res =
StreamUtils.interleave(alternating, Arrays.asList(list1.stream(), list2.stream()))
.collect(Collectors.toList());
Assert.assertEquals(Arrays.asList(cn11, cn21, cn12, cn22), res);
} fails with the Shifting to generic collections instead of arrays would fix this, but it would also break any user-written selectors that worked with arrays. I'm also not so keen on paying the allocation cost of a new interface Selectable<T> {
int range(); // the range of indices to select from
boolean hasValue(int index); // whether there is a value at the given index
T getValue(int index); // get the value at the given index (or throw some exception if there isn't one)
} and having |
(the answer to "why does this happen" is that the generic |
I was under the impression that the buffer creation in a lambda (
Absolutely, creating a new ArrayList is a no go. Wrapping the array with
Using an interface is a good idea. Maybe Optional as a return value to communicate the intent better? But this would break already created selector unless we support both. |
Method StreamUtil.interleaving() seems to be broken in v1.14. I keep getting this exception:
The following unit test reproduces this:
I get no compile errors for this test, but the strange ClassClast at runtime.
The text was updated successfully, but these errors were encountered: