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

GROOVY-7774: Collection addAll fails CompileStatic type checking when… #376

Closed
wants to merge 2 commits into from

Conversation

paulk-asert
Copy link
Contributor

… adding a collection of subtypes

@jwagenleitner
Copy link
Contributor

+1

@paulk-asert
Copy link
Contributor Author

I added in some Map methods which suffered the same problem

final Map<G, Map<K, V>> answer = new LinkedHashMap<G, Map<K, V>>();
for (Map.Entry<G, List<Map.Entry<K, V>>> outer : initial.entrySet()) {
public static <G, K, V> Map<G, Map<? extends K, ? extends V>> groupBy(
Map<K, V> self, @ClosureParams(MapEntryOrKeyValue.class) Closure<G> closure) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the wildcards be on the self parameter instead? Just not used to seeing them on return types, but nested wildcards often confuse me 😄

Would the closure parameter benefit from having ? extends G?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind my comments on wildcards on self and closure params, the more I think about it that doesn't make sense.

However, wont changing the return type to have wildcards extends mean that nothing can be put in the map that is mapped to the groupBy key which some may rely on being able to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't exactly what I would like but groupBy calls putAll and the other changes are to make Java happy. We might have to do some casting and suppress some warnings instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should use the following guideline: no wildcards on self or returns, only on parameters we use as input. In mutating calls, even the (in/)out parameters should not have wildcards. Because wildcards make it very very difficult to use the container for any adding.

… adding a collection of subtypes (same treatment for some Map methods)
@paulk-asert
Copy link
Contributor Author

I removed the groupBy changes and inlined a few lines instead.

@@ -5176,7 +5176,9 @@ public static Map groupBy(Object[] self, List<Closure> closures) {
G key = outer.getKey();
List<Map.Entry<K, V>> entries = outer.getValue();
Map<K, V> target = createSimilarMap(self);
putAll(target, entries);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you removed the putAll here because of generics?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but feel free to play around with other options if you have a strong preference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my suggestion would be to change putAll to public static <K, V> Map<K, V> putAll(Map<K, V> self, Collection<? extends Map.Entry<? extends K, ? extends V>> entries)
which more or less adds ? extends before the Map.Entry part and to change back to use the putAll instead of the loop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice suggestion. Done.

asfgit pushed a commit that referenced this pull request Aug 1, 2016
… adding a collection of subtypes (same treatment for some Map methods) (closes #376)
@asfgit asfgit closed this in a0aee5b Aug 1, 2016
@paulk-asert paulk-asert deleted the groovy7774 branch August 2, 2016 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants