You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2020/06/13 18:13:00 UTC

[jira] [Created] (GROOVY-9591) Compiler error for use of variable expression within tap closure

Eric Milles created GROOVY-9591:
-----------------------------------

             Summary: Compiler error for use of variable expression within tap closure
                 Key: GROOVY-9591
                 URL: https://issues.apache.org/jira/browse/GROOVY-9591
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 2.5.12, 3.0.4
            Reporter: Eric Milles
            Assignee: Eric Milles


Consider the following:
{code:groovy}
@groovy.transform.ToString
class A {
  A(a) {}
  def b
}
class C {
  C() {
    this(new A(null).tap { b = 42 })
  }
C(x) {
    print x
  }
}
new C() // should output "A(42)"
{code}

A has no default constructor, so properties are initialized in tap closure.  Because the "tap" expression is passed as argument to special constructor call, extra static checking is performed and the error "Apparent variable 'b' was found in a static scope but doesn't refer to a local variable, static field or class." is emitted.

Creation of A cannot be moved to a local variable because special constructor call must be the first statement/expression in the constructor body.

This was discovered while investigating variations of GROOVY-8327, GROOVY-8389, and GROOVY-9587. 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)