A workaround is to cast, e.g.

list[1] = (String) null

I don't see why that is needed for null. On GROOVY_4_0_X, the map also
needs the cast. On master, only the list needs the cast. I can't
explain that either (yet).

Paul.

On Mon, Apr 14, 2025 at 5:37 PM Per Nyfelt <per.nyf...@nordnet.se> wrote:
>
> Hi,
>
> I was recently experimenting with @CompileStatic for performance improvements 
> and noticed that when using the short notation of putAt to assign a null 
> value I get the following error
>
> [Static type checking] - Cannot call <T> 
> org.codehaus.groovy.runtime.DefaultGroovyMethods#putAt(java.util.List<T>, 
> int, T) with arguments [java.util.List<java.lang.String>, int, 
> java.lang.Object]
>
>
>
> Here is an example:
>
>
>
> import groovy.transform.CompileStatic
> import org.junit.jupiter.api.Test
>
> @CompileStatic
> class PutAtTest {
>
>   @Test
>   void testList() {
>     // These all work
>     def list = ['a', 'b', 'c']
>     list[0] = 'aa'
>     assert list[0] == 'aa'
>
>     list.set(2, null)
>     assert list[2] == null
>
>     list.putAt(0, null)
>     assert list[0] == null
>
>     // This Fails
>     list[1] = null
>     assert list[1] == null : "Short notation not working when assigning null"
>   }
>
>   @Test
>   void testMap() {
>     // These all work
>     def map = [a: 'foo', b: 'bar', c: 'baz']
>     map['a'] = 'aa'
>     assert map['a'] == 'aa'
>
>     map.put('c', null)
>     assert map['c'] == null
>
>     map.putAt('a', null)
>     assert map['a'] == null
>
>     // This Fails
>     map['b'] = null
>     assert map['b'] == null  : "Short notation not working when assigning 
> null"
>   }
> }
>
>
>
> Is this expected behavior or a bug? Does anyone know of workaround?
>
>
>
> Regards,
>
> Per

Reply via email to