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

Instanceof pattern: exclusion for Stream.collect(..) #337

Closed
wants to merge 2 commits into from

Conversation

BoykoAlex
Copy link
Contributor

Support instanceof with types where type parameters shouldn't be present in the instanceof.

For example:

if (o instanceof List) {
  map.put("key", String.join(",", (List) o));
}

currently produces a compiler error when refactored in:

if (o instanceof List<?> list) {
  map.put("key", String.join(",", list));
}

The compiler error:

The method join(CharSequence, CharSequence...) in the type String is not applicable for the arguments (String, List<capture#1-of ?>)

Instead it should become:

if (o instanceof List list) {
  map.put("key", String.join(",", list));
}

(i.e. without the wildcard)

The fix is if the type has type parameters and the following type casts statements cast to type with type parameters then wildcard type parameter(s) will be added. Otherwise no type parameters will be added to the type in the instanceof

@BoykoAlex BoykoAlex marked this pull request as draft September 13, 2024 22:17
@timtebeek timtebeek self-requested a review September 13, 2024 22:24
@BoykoAlex BoykoAlex changed the title Instanceof pattern: add type parameters if type casts use type parame… Instanceof pattern: exclusion for Stream.collect(..) Sep 17, 2024
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek
Copy link
Contributor

Thanks for the work done here @BoykoAlex ! Let me know when you're done by marking the PR as ready for review. :)

@BoykoAlex
Copy link
Contributor Author

Abandoned in favour of #338

@BoykoAlex BoykoAlex closed this Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants