Basel Shishani
Basel Shishani

Reputation: 8187

Error when referencing an attribute of type Map inside a closure

In the following code segment, why does the compiler complain about the map attribute but not other types of attributes:

import groovy.transform.CompileStatic

@CompileStatic
class TestMapInClosure {

    Map amap = [:]
    List alist = []
    Integer intval = 0

    Closure doFoo = {           
        this.amap['one'] = 'two'  // !! [Static type checking] - No such property
        this.alist.push(1)
        this.intval += 5
    }
}

this inside a closure is supposed to refer to the instance of the enclosing class, if I understand things correctly.

Note: Groovy Version: 2.4.5

Upvotes: 3

Views: 100

Answers (1)

tim_yates
tim_yates

Reputation: 171144

Looks like a bug in the CompileStatic annotation, as if you change the line to:

this.amap += [one:'two']

Or

this.amap.one = 'two'

Then it works fine. I'm guessing it's due to the semantics of the [] map accessor.

You could sumbit it as a bug and see if it can be fixed

Upvotes: 1

Related Questions