You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by OCsite <oc...@ocs.cz> on 2025/01/28 07:53:05 UTC

@NamedVariant vs @VisibilityOptions

Hi there,

do I make some mistake not understanding properly those two ASTTs, or is this a Groovy bug?

Thanks!
OC

===
975 ocs /tmp> <nv.groovy 
import groovy.transform.*
import groovy.transform.options.*
class Test {
  @NamedVariant @VisibilityOptions(Visibility.PUBLIC) private makeSense(int dollars, int cents) {
    "d:$dollars c:$cents"
  }
}
def t=new Test()
println t.makeSense(dollars: 2, cents: 50)
976 ocs /tmp> /usr/local/groovy-4.0.24/bin/groovy nv
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/private/tmp/nv.groovy: 4: Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour. Renaming the private methods will solve the problem.
 @ line 4, column 3.
     @NamedVariant @VisibilityOptions(Visibility.PUBLIC) private makeSense(int dollars, int cents) {
     ^

/private/tmp/nv.groovy: -1: Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour. Renaming the private methods will solve the problem.
 @ line -1, column -1.
2 errors

977 ocs /tmp> 
===

Re: @NamedVariant vs @VisibilityOptions

Posted by OCsite <oc...@ocs.cz>.
Paul,

thanks for the explanation!

In my particular case I can do with protected or even public; it's just that the documentation quite explicitly says “You could have the annotated method/constructor private for instance but have the generated one be public” (https://docs.groovy-lang.org/latest/html/gapi/groovy/transform/NamedVariant.html), so I've tried that.

I guess in this case, the code's all right, and it's the documentation what needs a small fix :)

Thanks a lot,
OC

> On 28. 1. 2025, at 12:11, Paul King <pa...@asert.com.au> wrote:
> 
> It is more a case of the AST transforms running into a Groovy feature.
> Groovy doesn't let you compile this class:
> 
> class Foo {
>    private foo() {}
>    public foo(String s) {}
> }
> 
> It might seem strange but it has been that way for a long time.
> 
> Can you change "makeSense" to protected?
> 
> Paul.
> 
> On Tue, Jan 28, 2025 at 5:53 PM OCsite <oc...@ocs.cz> wrote:
>> 
>> Hi there,
>> 
>> do I make some mistake not understanding properly those two ASTTs, or is this a Groovy bug?
>> 
>> Thanks!
>> OC
>> 
>> ===
>> 
>> 975 ocs /tmp> <nv.groovy
>> 
>> import groovy.transform.*
>> 
>> import groovy.transform.options.*
>> 
>> class Test {
>> 
>>  @NamedVariant @VisibilityOptions(Visibility.PUBLIC) private makeSense(int dollars, int cents) {
>> 
>>    "d:$dollars c:$cents"
>> 
>>  }
>> 
>> }
>> 
>> def t=new Test()
>> 
>> println t.makeSense(dollars: 2, cents: 50)
>> 
>> 976 ocs /tmp> /usr/local/groovy-4.0.24/bin/groovy nv
>> 
>> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
>> 
>> /private/tmp/nv.groovy: 4: Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour. Renaming the private methods will solve the problem.
>> 
>> @ line 4, column 3.
>> 
>>     @NamedVariant @VisibilityOptions(Visibility.PUBLIC) private makeSense(int dollars, int cents) {
>> 
>>     ^
>> 
>> 
>> /private/tmp/nv.groovy: -1: Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour. Renaming the private methods will solve the problem.
>> 
>> @ line -1, column -1.
>> 
>> 2 errors
>> 
>> 
>> 977 ocs /tmp>
>> 
>> ===


Re: @NamedVariant vs @VisibilityOptions

Posted by Paul King <pa...@asert.com.au>.
It is more a case of the AST transforms running into a Groovy feature.
Groovy doesn't let you compile this class:

class Foo {
    private foo() {}
    public foo(String s) {}
}

It might seem strange but it has been that way for a long time.

Can you change "makeSense" to protected?

Paul.

On Tue, Jan 28, 2025 at 5:53 PM OCsite <oc...@ocs.cz> wrote:
>
> Hi there,
>
> do I make some mistake not understanding properly those two ASTTs, or is this a Groovy bug?
>
> Thanks!
> OC
>
> ===
>
> 975 ocs /tmp> <nv.groovy
>
> import groovy.transform.*
>
> import groovy.transform.options.*
>
> class Test {
>
>   @NamedVariant @VisibilityOptions(Visibility.PUBLIC) private makeSense(int dollars, int cents) {
>
>     "d:$dollars c:$cents"
>
>   }
>
> }
>
> def t=new Test()
>
> println t.makeSense(dollars: 2, cents: 50)
>
> 976 ocs /tmp> /usr/local/groovy-4.0.24/bin/groovy nv
>
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
>
> /private/tmp/nv.groovy: 4: Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour. Renaming the private methods will solve the problem.
>
>  @ line 4, column 3.
>
>      @NamedVariant @VisibilityOptions(Visibility.PUBLIC) private makeSense(int dollars, int cents) {
>
>      ^
>
>
> /private/tmp/nv.groovy: -1: Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour. Renaming the private methods will solve the problem.
>
>  @ line -1, column -1.
>
> 2 errors
>
>
> 977 ocs /tmp>
>
> ===