You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Dmitry Bobrov (JIRA)" <ji...@apache.org> on 2016/12/30 10:04:58 UTC

[jira] [Comment Edited] (GROOVY-8029) Static compilation fails when using collectMany and elvis operator

    [ https://issues.apache.org/jira/browse/GROOVY-8029?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15787359#comment-15787359 ] 

Dmitry Bobrov edited comment on GROOVY-8029 at 12/30/16 10:04 AM:
------------------------------------------------------------------

[~jwagenleitner], I guess, the type should be {{Closure<? extends Collection<? extends T>>}}. But neither of the signature changes would help much until type inference with elvis operator is fixed:

{code}
static test(List<Object> list) {
    List<CharSequence> res = collectManyCorrect(list) {
        return [it.toString() /* can't be compiled without 'as CharSequence' */]
    } ?: ([] as List<CharSequence>)
}

static <S, T> List<T> collectManyCorrect(List<S> list, Closure<? extends Collection<T>> closure) {
    List<T> res = []
    for (S val : list) {
        res.addAll(closure(val))
    }
    return res
}
{code}


was (Author: dvbobrov):
[~jwagenleitner], I guess, the type should be {{Closure<? extends Collection<? extends T>>}}. But neither of the signature changes would help much until type inference with elvis operator is fixed:

{code}
static test(List<Object> list) {
    List<CharSequence> res = collectManyCorrect(list) {
        return [list.toString() /* can't be compiled without 'as CharSequence' */]
    } ?: ([] as List<CharSequence>)
}

static <S, T> List<T> collectManyCorrect(List<S> list, Closure<? extends Collection<T>> closure) {
    List<T> res = []
    for (S val : list) {
        res.addAll(closure(val))
    }
    return res
}
{code}

> Static compilation fails when using collectMany and elvis operator
> ------------------------------------------------------------------
>
>                 Key: GROOVY-8029
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8029
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 2.4.7
>            Reporter: Dmitry Bobrov
>            Priority: Minor
>
> The following code, when used with {{\@CompileStatic}}, produces {{Groovyc: [Static type checking] - Cannot call <T,E> java.util.List <Object>#collectMany(groovy.lang.Closure <java.util.Collection>) with arguments [groovy.lang.Closure <java.util.List>]}}
> {code}
> List<String> getStrings(List<Object> l) {
>     l.collectMany { [it.toString()] } ?: ([] as List<String>)
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)