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

Anonymous classes do not produce members when constructed as a function argument #5448

Closed
scabug opened this issue Feb 9, 2012 · 5 comments

Comments

@scabug
Copy link

scabug commented Feb 9, 2012

If an anonymous class is instantiated as an argument to a function call,
its members will not be available unless they have been assigned to a variable.

def getFoo(a: Any) {
  try {
    a.getClass.getMethod("foo")
    println("Foo found!")
  }
  catch {
    case e: java.lang.NoSuchMethodException =>
      println("Foo missing!")
  }
}

getFoo(
  new { def foo = 1 }         // this will be left out unless the function 
)                             // requires the foo method via a structural type

getFoo{
  val a = new { def foo = 1 } // this will work because the compile
  a                           // doesn't know where 'a' will be used
}
{code} 
@scabug
Copy link
Author

scabug commented Feb 9, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5448?orig=1
Reporter: @melezov
Affected Versions: 2.9.1, 2.10.0-M1
See #3174, #3560
Attachments:

@scabug
Copy link
Author

scabug commented Feb 14, 2012

@cvogt said:
Is this something relevant to your thoughts about a structural type re-design? Also, would this be a wontfix, suggesting to use the upcoming scala reflection of 2.10 instead?

@scabug
Copy link
Author

scabug commented Feb 14, 2012

@adriaanm said:
this is Skylla and Charybdis territory (github.com/scala/scala/commit/d300a5817fb4826516e0bc15b1e279903946a524)

symptomatic explanation:
it's private in the first case since the type does not escape
in the second case, we have to come up with a type, and we include foo

[adriaan@lampmac13 scala (topic/virtpatmat)]$ qsc /Users/adriaan/Desktop/t5448.scala -Xprint:typer
[[syntax trees at end of typer]]// Scala source: t5448.scala
package <empty> {
  final object Test extends Object with App with ScalaObject {
    def <init>(): object Test = {
      Test.super.<init>();
      ()
    };
    def getFoo(a: Any): Unit = try {
      a.getClass().getMethod("foo");
      scala.this.Predef.println("Foo found!")
    } catch {
      case (e @ (_: NoSuchMethodException)) => scala.this.Predef.println("Foo missing!")
    };
    Test.this.getFoo({
      final class $anon extends scala.AnyRef {
        def <init>(): anonymous class $anon = {
          $anon.super.<init>();
          ()
        };
        private def foo: Int = 1
      };
      new $anon()
    });
    Test.this.getFoo({
      val a: Object{def foo: Int} = {
        final class $anon extends scala.AnyRef {
          def <init>(): anonymous class $anon = {
            $anon.super.<init>();
            ()
          };
          def foo: Int = 1
        };
        new $anon()
      };
      a
    })
  }
}

[adriaan@lampmac13 scala (topic/virtpatmat)]$ qs Test
Foo missing!
Foo found!

@scabug
Copy link
Author

scabug commented Feb 14, 2012

@adriaanm said:
thus, the easy workaround is to change getFoo's signature to force the compiler to come up with a type for getFoo's argument

  def getFoo[T](a: T)

@scabug scabug closed this as completed Feb 21, 2012
@scabug
Copy link
Author

scabug commented Apr 17, 2014

@Blaisorblade said:
My analysis of the issue is that : the broken equivalence is (a: Any) <=> a, which is something Eugene and I also discussed in #6155.

https://issues.scala-lang.org/browse/SI-6155?focusedCommentId=59549&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-59549

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

No branches or pull requests

1 participant