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/04/26 03:15:00 UTC

[jira] [Commented] (GROOVY-5893) Return type of "inject" is not inferred properly

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

Eric Milles commented on GROOVY-5893:
-------------------------------------

Given the current declaration of {{inject}}, {{T}} is only determined by the target of the return type.  The declaration states that {{T}} must be equal to or a shared parent of {{U}} and {{V}}.  If you were to use integers, you could assign the return to Integer, Number, Object, Comparable, Serializeable and so on.
{code:java}
public static <E, T, U extends T, V extends T> T inject(Collection<E> self, U initialValue, @ClosureParams(value=FromString.class,options="U,E") Closure<V> closure)
{code}

So, this should produce an error:
{code:groovy}
Double result = [1,2,3].inject(0) { int total, int i -> total += i }
{code}

But this does not provide for inference of {{int}} or {{Integer}} for "result":
{code:groovy}
def result = [1,2,3].inject(0) { int total, int i -> total += i }
{code}

I think it is strange that no relation between the initial value type {{U}} and the step result type {{V}} exists, except through {{T}}.  The step function could return something that is not compatible with {{U}}.

> Return type of "inject" is not inferred properly
> ------------------------------------------------
>
>                 Key: GROOVY-5893
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5893
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 2.0.6, 2.3.9
>            Reporter: Cédric Champeau
>            Priority: Major
>
> The return type of {{inject}} is not inferred properly. Currently, there's no rule allowing the type checker to infer the type. The signature of the method is:
> {code}
> public static <T, U extends T, V extends T> T inject(Collection self, U initialValue, Closure<V> closure)
> {code}
> So both {{U}} and {{V}} must be took into account to be able to infer the return type {{T}}.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)