groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Duncan Dickinson <>
Subject Instance initializer blocks confused for a closure
Date Sat, 11 Jul 2015 05:48:45 GMT
Hi all,

I've been writing about object/instance initializer blocks in Groovy 2.4.3
and have noticed that an exception is raised depending on the layout I use
in declaring properties and using an initializing block. I note that this
was raised in 2006:

I thought I'd send through some basic sample code to demonstrate the issue.
This issue only occurs for instance initializer blocks. Static initializers
don't have the same problem, possible as they have the 'static' prefix,
helping to delimit the previous statement.

Should this be a Jira ticket (I couldn't see one), if only to document it?
It's something I noticed rather than something that upsets me.

*Example 1: Works.*
I declare two properties with no initial assignment. Assignment performed
in initializer block.

class Person1 {
    def id
    def name

        id = 1
        name = 'Anonymous'

Person1 p1 = new Person1()
println p1.dump()

*Example 2: Works.*
I initialize the id property but not the name property.

class Person2 {
    def id = 2
    def name

        name = 'Anonymous2'

Person2 p2 = new Person2()
println p2.dump()

*Example 3: Causes **exception* groovy.lang.MissingMethodException: No
signature of method: Person3.Anonymous3() is applicable for argument types:

class Person3 {
    def id
    def name = 'Anonymous3'

        id = 3

Person3 p3 = new Person3()

*Example 4: Works*
This is the same as Example 3 except I place a semicolon to explicitly
terminate the statement

class Person4 {
    def id
    def name = 'Anonymous4';

        id = 4

Person4 p4 = new Person4()

*Example 5: Works*
Similar to Example 3 - I just place another statement after the assignment
and the initializer block isn't mistaken for a closure

class Person5 {
    def id
    def name = 'Anonymous5'
    def email

        id = 5

Person5 p5 = new Person5()



View raw message