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

Eric Milles commented on GROOVY-11749:
--------------------------------------

I need more context around the setHeader list-to-array call, as my example does 
the string conversion as expected.
{code:groovy}
@CompileStatic foo(Object object) {
  if (object instanceof List) {
    bar(object as String[])
  }
}
void bar(String... strings) {
  print(strings[0])
}
foo(['fizz','buzz'])
{code}

> CompileStatic issues in Groovy 5
> --------------------------------
>
>                 Key: GROOVY-11749
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11749
>             Project: Groovy
>          Issue Type: Bug
>         Environment: Groovy 5.0.0, Gradle 8.14.3,  JVM: 21.0.6
>            Reporter: Per Nyfelt
>            Priority: Major
>
> When working on getting the Matrix project to work with Groovy 5 i 
> encountered the following differences compared to 4.0.28 for static 
> compilation:
> f is a csv commons formatter. The setHeader method has the following 
> signature:
> f.setHeader(String... headers)
> In 4.0.28 i used to be able to just cast a List<String> to a String[] like 
> this:
> if (val instanceof List) {
>   f.setHeader({*}val as String[]{*})
>  
> But in Groovy 5 this gives me the following error:
> java.lang.ClassCastException: class java.util.ArrayList cannot be cast to 
> class [Ljava.lang.String; (java.util.ArrayList and [Ljava.lang.String; are in 
> module java.base of loader 'bootstrap')
>  
> A workaround is to use the Java way i.e:
> if (val instanceof List) {
>   f.setHeader(val.toArray({*}new String[0]) as String[{*}])
> Another thing i hade to change was
> static String asColumnName(int number) {
>   StringBuilder sb = new StringBuilder()
>   while (number-- > 0) {
>     sb.append(('A' as char + (number % 26)) as char)
>     *number /= 26*
>   }
>   return sb.reverse().toString()
> }
> Which no longer compiles:
> .../SpreadsheetUtil.groovy: 45: [Static type checking] - Cannot assign value 
> of type java.math.BigDecimal to variable of type int
> but had to be changed to 
> static String asColumnName(int number) {
>   StringBuilder sb = new StringBuilder()
>   while (number-- > 0) {
>     sb.append(('A' as char + (number % 26)) as char)
>     *number = (int) (number / 26)*
>   }
>   return sb.reverse().toString()
> }
>  



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

Reply via email to