[ https://issues.apache.org/jira/browse/GROOVY-11698?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17959818#comment-17959818 ]
Eric Milles commented on GROOVY-11698: -------------------------------------- I scoped the variable ton the declaring statement. This seemed sufficient. The goal was not 100% java compatibility. > Instanceof pattern variable doesn't respect scope > ------------------------------------------------- > > Key: GROOVY-11698 > URL: https://issues.apache.org/jira/browse/GROOVY-11698 > Project: Groovy > Issue Type: Bug > Components: class generator > Reporter: Georgii Ustinov > Priority: Minor > > Hi, I am main supporter of Groovy plugin inside IntelliJ IDEA IDE and > currently processing > [instanceof variable > support.|https://youtrack.jetbrains.com/issue/IDEA-371239/Groovy-Support-instanceof-variable] > I have identified several cases in which pattern matching inside instanceof > doesn't respect the scope and behaviour differs from Java. > See the examples: > 1. Instanceof in else branch > Java: > {code:java} > package org.java; > public class MainJava { > static class A {} > static class B extends A{} > public static void main(String[] args) { > A a = new B(); > if (!(a instanceof B b)) { > } else { > System.out.println(b); > } > } > } > {code} > Groovy (doesn't compile): > {code:java} > package org.groovy > class Main { > static class A {} > static class B extends A {} > static void main(String[] args) { > A a = new B(); > if (!(a instanceof B b)) { > } else { > println b > } > } > } > {code} > 2. Instanceof in return statements: > Java > {code:java} > package org.java; > public class MainJava { > static class A {} > static class B extends A{} > public static void main(String[] args) { > A a = new B(); > if (!(a instanceof B b)) { > return; > } > System.out.println(b); > } > }{code} > Groovy (doesn't compile) > {code:java} > package org.groovy > class Main { > static class A {} > static class B extends A {} > static void main(String[] args) { > A a = new B(); > if (!(a instanceof B b)) { > return > } > println b > } > } > {code} > 3. Negations after loop > Java > {code:java} > package org.java; > public class MainJava { > static class A {} > static class B extends A{} > public static void main(String[] args) { > A a = new B(); > while (!(a instanceof B b)) { > return; > } > System.out.println(b); > } > } > {code} > Groovy (doesn't compile) > {code:java} > package org.groovy > import org.java.MainJava > class Main { > static class A {} > static class B extends MainJava.A{} > static void main(String[] args) { > A a = new B() > while (!(a instanceof B b)) { > } > println b > } > } {code} > Could you, please, fix these issues? > > -- This message was sent by Atlassian Jira (v8.20.10#820010)