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

Paul King commented on GROOVY-11737:
------------------------------------

The order as described above is correct in terms of an implementation detail 
for selecting whether we have a script, or a traditionally supported main 
method script, or a JEP-512 main method. Currently, the particular selected 
method isn't remembered, just the fact that a suitable main is there, and then 
normal method selection applies. This selection process accounts for 
multimethods.

For "normal method selection", inside MetaClassImpl#chooseMethodInternal, there 
is special logic that picks the best matching method ("main" method in our 
case) when multiple are suitable.
If no arguments are given special matching picks the no-arg variant:
[https://github.com/apache/groovy/blob/master/src/main/java/groovy/lang/MetaClassImpl.java#L3235-L3236]
If an argument is given, the method you expect is matched.

If you have a third main method in that same script:
{code:groovy}
static void main(String a, String b) {
  println "3"
}
{code}
And call like this:
{noformat}
groovy main.groovy x y
{noformat}
Then 3 will be printed even though it is never a method that is selected by the 
above selection process.

Scripts containing multiple main methods would often be regarded as poor style, 
but I agree we should specify/document how we handle that case once determining 
whether this is the behavior we want. I'll have a bit more of a think and 
potentially start a discussion on the mailing list.

> Understanding Groovy main method priority overloading.
> ------------------------------------------------------
>
>                 Key: GROOVY-11737
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11737
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Georgii Ustinov
>            Assignee: Paul King
>            Priority: Major
>
> Good afternoon
>  
> I am currently trying to support instance main methods inside IntelliJ IDEA 
> groovy plugin as a part of Groovy 5 release, however, I can’t find any 
> specification of how it works.
>  
> I have found that the feature was implemented here - 
> [https://github.com/apache/groovy/pull/1910/files].
>  
> Looking into the source code - 
> [https://github.com/paulk-asert/groovy/blob/90480bfade8c20f935311055e2ceb85288870605/src/main/java/groovy/lang/GroovyShell.java#L275]
>  I can see the following order:
>  
> 1) static void main(String[] args);
> 2) void main(String[] args);
> 3) static void main(args);
> 4) void main(args);
> 5) static void main();
> 6) void main();
>  
> The problem is that it doesn’t correspond to the reality.
>  
> Let’s consider the following code main.groovy
>  
>  
> {code:java}
> void main(args) {
> println "1"
> }
>  
> static void main() {
> println "2"
> }
> {code}
>  
>  
> If I run 
> {code:java}
> groovy main.groovy{code}
> 1 will be printed (and it looks correct according to the semantics),
> But if I change example:
>  
>  
> {code:java}
> void main(String[] args) {
> println "1"
> }
>  
> static void main() {
> println "2"
> }
> {code}
> 2 will be printed (and thus the order in the code is violated). But if I run 
> {code:java}
> groovy main.groovy someArg{code}
> , then 1 will be printed
>  
> Could you, please, provide the correct main method order resolution?



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

Reply via email to