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

Eric Milles commented on GROOVY-10668:
--------------------------------------

In contrast to the Java compiler, the Groovy compiler applies typecasts 
implicitly in {{instanceof}} and other cases.  So your example does not require 
a typecast for {{List}}:
{code:groovy}
@groovy.transform.CompileStatic
  def toArray(Object value) {
    def res
    if (value instanceof List)
      res = value.toArray() // compiler knows value is a List
    else if (value instanceof String || value instanceof GString)
      res = value.toString().split(',') // compiler knows value is Object and 
String or GString -- the ambiguous reference comes when both String and GString 
declare a method (like "toString()").
    else
      throw new Exception("Not support!")
    return res
  }
}
{code}

In this specific example, you could replace the String/GString guard with 
CharSequence.  Or you could replace "value.toString()" with "(value as String)" 
and you would not have to worry about Groovy 4+ resurfacing this issue.

> Reference to method is ambiguous if block in "if" with "instanceof"
> -------------------------------------------------------------------
>
>                 Key: GROOVY-10668
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10668
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 3.0.11
>            Reporter: Alexsey Konstantinov
>            Assignee: Eric Milles
>            Priority: Blocker
>             Fix For: 3.0.12
>
>
> In version 3.0.10 normally the following code worked fine:
> {noformat}
> import groovy.transform.CompileStatic
> @CompileStatic
> class Test {
>   def toArray(Object value) {
>     def res
>     if (value instanceof List)
>       res = (value as List).toArray()
>     else if (value instanceof String || value instanceof GString)
>       res = value.toString().split(',')
>     else
>       throw new Exception("Not support!")
>     return res
>   }
> }
> println new Test().toArray([1,2,3])
> println new Test().toArray('1,2,3'){noformat}
> {{In version 3.0.11, the code does not compile with the following error:}}
>  
> {noformat}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
> failed:
> ideaGroovyConsole.groovy: 10: [Static type checking] - Reference to method is 
> ambiguous. Cannot choose between [java.lang.String 
> java.lang.String#toString(), java.lang.String groovy.lang.GString#toString()]
>  @ line 10, column 33.
>                res = value.toString().split(',')
>                                    ^
> ideaGroovyConsole.groovy: 10: [Static type checking] - Cannot find matching 
> method java.lang.Object#split(java.lang.String). Please check if the declared 
> type is correct and if the method exists.
>  @ line 10, column 19.
>                res = value.toString().split(',')
>                      ^
> {noformat}
> If you remove _value instanceof GString_ from "if", then the code compiles 
> and runs.



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

Reply via email to