David Padbury
David Padbury

Reputation: 1182

Using a Closure as an argument to a superclass constructor

I seem unable to use a Closure as a parameter to a superclass constructor when it is specified inline.

class Base {

  def c

  Base(c) {
    this.c = c
  }

  void callMyClosure() {
    c()
  }
}

class Upper extends Base {
  Upper() {
    super( { println 'called' } )
  }
}

u = new Upper()
u.callMyClosure()

The compilation fails with the message Constructor call must be the first statement in a constructor..

I realize this a somewhat strange use-case, and I can design around it for the time being. But I'm interested, is this is to be expected? Or have I got the syntax incorrect?

Upvotes: 3

Views: 855

Answers (2)

Jack
Jack

Reputation: 133587

I think that the problem is related to the fact that Groovy turns the constructor into something different while trying to compile it as a Java class. Maybe the closure definition is expanded before the call to super generating that error.

A workaround is to define the closure outside the constructor itself:

class Base {
  def c

  Base(c) {this.c = c}


  void callMyClosure() {
    c()
  }
}

class Upper extends Base {
  static cc = {println 'called'}

  Upper() {
    super(cc)
  }
}

u = new Upper()
u.callMyClosure()

It's not so nice but at least it works.. another way could be to define the closure by using a normal new Closure(...) syntax

Upvotes: 1

tim_yates
tim_yates

Reputation: 171104

It might be confusing a closure and a block...can you try

super( { -> println 'called' } )

Upvotes: 0

Related Questions