Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type inferencing and syntax highlighting for anon. inner in with block #927

Closed
eric-milles opened this issue Jul 23, 2019 · 1 comment
Assignees
Labels
Milestone

Comments

@eric-milles
Copy link
Member

Consider the following:

void meth() {
  org.xml.sax.helpers.XMLReaderFactory.createXMLReader().with {
    contentHandler = new org.xml.sax.helpers.DefaultHandler() {
      private Stack<StringBuilder> chars = []

      @Override
      void characters(char[] text, int offset, int length)
      {
        if (chars) chars.peek().append(text, offset, length)
      }
    }
  }
}

The variable expressions "chars" within the "characters" method are showing as unknown (underlined).

@eric-milles eric-milles self-assigned this Jul 23, 2019
@eric-milles
Copy link
Member Author

eric-milles commented Sep 10, 2019

Looks like the anon. inner class does not have access to the enclosing closure's delegate/owner/thisObject resolution.

class Foo {
  String string = 'foo'
}
new Foo().with {
  def bar = new Object() {
    String toString() { string }
  }
  println bar
}
groovy.lang.MissingPropertyException: No such property: string for class: Script1
	at Script1$1.toString(Script1.groovy:8)
	at Script1$_run_closure1.doCall(Script1.groovy:10)
	at Script1.run(Script1.groovy:5)

However, I can still access "it" (or any closure parameters if they had been declared).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant