Skip to content
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

Issue with java wildcard #236

Closed
foal opened this issue Nov 19, 2020 · 1 comment
Closed

Issue with java wildcard #236

foal opened this issue Nov 19, 2020 · 1 comment

Comments

@foal
Copy link

foal commented Nov 19, 2020

Have a simple code

    @SuppressWarnings("resource")
    private static <R> List<R> getMissingItems(List<? extends R> freshItems, List<? extends R> oldItems, 
                                                                           Function<? super R, String> idFunc) {
        List<String> idList = StreamEx.of(freshItems).map(idFunc::apply).toList();
        //Java stream works
        List<R> result = oldItems.stream()
            .filter(e -> !idList.contains(idFunc.apply(e)))
            .collect(Collectors.toList());
        //StreamEx - Type mismatch: cannot convert from List<capture#19-of ? extends R> to List<R>
        result = StreamEx.of(oldItems)
            .remove(e -> idList.contains(idFunc.apply(e)))
            .toList();
        return result;
    }

So part with StreamEx can't be compiled due to the error: Type mismatch: cannot convert from List<capture#19-of ? extends R> to List<R>

@amaembo
Copy link
Owner

amaembo commented Nov 20, 2020

Yes, it's an unfortunate consequence of Java type system and there's nothing that could be fixed on the library side. You can avoid this problem adding a bogus .<R>map(x -> x) step or just using traditional .collect(Collectors.toList());. Note that it's planned to add to Java 16 a new toList() method right into the java.util.Stream, and it will suffer the same problem.

@amaembo amaembo closed this as completed Nov 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants