Skip to content

Scope of anonymous class methods are not consistent #10264

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

Closed
zsxwing opened this issue Apr 13, 2017 · 2 comments
Closed

Scope of anonymous class methods are not consistent #10264

zsxwing opened this issue Apr 13, 2017 · 2 comments

Comments

@zsxwing
Copy link

zsxwing commented Apr 13, 2017

Methods in an anonymous class don't have consistent scope. In the following example, bar in foo is private, but in foo2, adding a temp variable will make it public.

Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.

scala> :paste
// Entering paste mode (ctrl-D to finish)

class A {}
def foo: A = new A { def bar(): Unit = {} }
def foo2: A = { 
  val temp = new A { def bar(): Unit = {} }
  temp
}

// Exiting paste mode, now interpreting.

defined class A
foo: A
foo2: A

scala> foo.getClass.getDeclaredMethods().filter(_.getName == "bar").toList
res0: List[java.lang.reflect.Method] = List(private void $anon$1.bar())

scala> foo2.getClass.getDeclaredMethods().filter(_.getName == "bar").toList
res1: List[java.lang.reflect.Method] = List(public void $anon$2.bar())

This is pretty annoying when trying to call bar using reflection. I expect in this example, bar should be public.

Scala version: 2.12.1
Java version: 1.8.0_45

@sjrd
Copy link
Member

sjrd commented Apr 13, 2017

This is a duplicate of #9487

@zsxwing
Copy link
Author

zsxwing commented Apr 13, 2017

Thanks! Closing it.

@zsxwing zsxwing closed this as completed Apr 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants