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 2022/02/15 23:56:00 UTC

[jira] [Commented] (GROOVY-9158) @NamedParam arguments don't get their default value when only named parameters are used in the method call

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

Eric Milles commented on GROOVY-9158:
-------------------------------------

https://github.com/apache/groovy/pull/1686

> @NamedParam arguments don't get their default value when only named parameters are used in the method call
> ----------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-9158
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9158
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.5.7
>            Reporter: Mathieu Grelier
>            Assignee: Eric Milles
>            Priority: Major
>              Labels: named-parameters
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> See the code below.
> @NamedParam only method signatures with default values for not required ones should be possible, with corresponding named parameters only method calls.
> {code:groovy}
> import groovy.transform.NamedParam
> import groovy.transform.NamedVariant
> import static groovy.test.GroovyAssert.shouldFail
>     
> class NamedParamTestClass {
>     Map getArgs(def arg1, def arg2) {
>         Map passedArgs = [:]
>         passedArgs << ["arg1":arg1, "arg2":arg2]
>         passedArgs
>     }
>      
>     @NamedVariant
>     Map onlyNamedArgs(@NamedParam(required = true) String arg1, @NamedParam(required = false) String arg2 = "expected default value") {
>         getArgs(arg1, arg2)
>     }
>     
>     @NamedVariant
>     Map primitiveTypeNotRequired(@NamedParam(required = false) String arg1, @NamedParam(required = false) int arg2 = 1) {
>         getArgs(arg1, arg2)
>     }
> }
> def test = new NamedParamTestClass()
> Map result
> result = test.onlyNamedArgs("a positional value for @NamedParam arg1")
> assert result.getAt("arg2") == "expected default value"
> result = test.onlyNamedArgs(arg1:"a named parameter value for @NamedParam arg1")
> shouldFail(AssertionError) {
>     assert result.getAt("arg2") == "expected default value" :  "arg2 was not given its default value"
> }
> assert result.getAt("arg2") == null
> shouldFail(MissingMethodException) {
>     result = test.primitiveTypeNotRequired(arg1:'1') // called with named arg
> }
> {code}
> Last test shows also that MissingMethodException occurs when a primitive type is used with @NamedParam.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)