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 2023/01/03 19:00:00 UTC

[jira] [Commented] (GROOVY-10889) Casting of arguments in @NamedVariant method has no effect

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

Eric Milles commented on GROOVY-10889:
--------------------------------------

{{NamedVariantASTTransformation}} is explicitly stripping the cast.  Looks like it only removes one, so you could double cast if until this is fixed.
{code:java}
    private static Expression earlierParamIfSeen(Map<Parameter, Expression> seen, Expression defValue) {
        if (seen == null) return defValue;
        // handle earlier param with or without cast
        if (defValue instanceof CastExpression) {
            defValue = ((CastExpression) defValue).getExpression();
        }
        return defValue instanceof VariableExpression ?
            seen.getOrDefault(((VariableExpression) defValue).getAccessedVariable(), defValue) : defValue;
    }
{code}

> Casting of arguments in @NamedVariant method has no effect
> ----------------------------------------------------------
>
>                 Key: GROOVY-10889
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10889
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 4.0.6
>            Reporter: Pavlo Shevchenko
>            Priority: Minor
>
> {code:java}
> import groovy.transform.NamedVariant
> class Reproducer {
>     @NamedVariant
>     private static Tuple2<Integer, Set<String>> createSampleData(
>         Integer left = 0,
>         Set<String> right = [] as Set
>     ) {
>         Tuple2.tuple(left, right)
>     }
>     static void main(String[] args) {
>         createSampleData(left: 1)
>     }
> }
> {code}
> The default value of "right" argument (i.e. "[] as Set") is detected as "ArrayList" despite the explicit cast. This used to work in Groovy 3.x, and now fails with
> {code:java}
> Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: static dev.pshevche.Reproducer.createSampleData() is applicable for argument types: (Integer, ArrayList) values: [1, []]{code}
> Replacing the "[] as Set" with "new HashSet<>()" resolves the issue.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)