You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2021/05/07 16:06:00 UTC
[jira] [Commented] (GROOVY-10074) DGM: Fix transform generics for
collect and similar
[ https://issues.apache.org/jira/browse/GROOVY-10074?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17340912#comment-17340912 ]
Eric Milles commented on GROOVY-10074:
--------------------------------------
Since the closure is the only type witness for "T" in the call arguments, I'm not sure how easily we could handle this since the target for the return value is not considered at the same time.
> DGM: Fix transform generics for collect and similar
> ---------------------------------------------------
>
> Key: GROOVY-10074
> URL: https://issues.apache.org/jira/browse/GROOVY-10074
> Project: Groovy
> Issue Type: Improvement
> Components: groovy-jdk
> Reporter: Eric Milles
> Priority: Minor
>
> The {{collect}} DGM is defined like this for {{Iterable}}, arrays, etc.:
> {code:java}
> <T> List<T> collect(Object self, Closure<T> transform)
> <S,T> List<T> collect(S[] self, @ClosureParams(FirstParam.Component.class) Closure<T> transform)
> <S,T> List<T> collect(Iterable<S> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<T> transform)
> <S,T> List<T> collect(Iterator<S> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure<T> transform)
> {code}
> and like this for the variants that accept an output collection:
> {code:java}
> <T> Collection<T> collect(Object self, Collection<T> collector, Closure<? extends T> transform)
> ...
> {code}
> I think {{Closure<? extends T>}} is better. It would allow plain {{collect}} to work for this example under STC without requiring {{? extends CharSequence}} on the target or a typecast for "it" or the call expression:
> {code:groovy}
> List<CharSequence> list = ['string'].collect { it }
> {code}
> For comparison, Java's {{Stream#map}} function is defined this way:
> {code:java}
> <R> Stream<R> map(Function<? super T, ? extends R> mapper)
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)