jack_kerouac
jack_kerouac

Reputation: 1522

Groovy static compilation (@CompileStatic) turns types of class fields into `Object`

Compiling the following Groovy class

@CompileStatic
class StaticCompileTest {

   private def fieldInt = 3

}

results in this class, when decompiling the .class file:

public class StaticCompileTest implements GroovyObject {
    private Object fieldInt;
}

Changing the field to be final does not change the situation.

Why is the Groovy compiler in this case not able to derive the type?

Upvotes: 1

Views: 214

Answers (1)

melix
melix

Reputation: 1530

Because it's inherently unsafe to assume that the type won't change.

see http://docs.groovy-lang.org/latest/html/documentation/#_variables_vs_fields_in_type_inference

Upvotes: 4

Related Questions