Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8284993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:18:44+00:00 2026-06-08T11:18:44+00:00

I expect this code (calling a method of an anonymous class after using a

  • 0

I expect this code (calling a method of an anonymous class after using a pattern match on refined type)

(new {
    def foo : Unit = println("Called foo method")
} : Any) match {
    case f : {def foo : Unit} ⇒
        println("Has foo method")
        f.foo
}

to print

Has foo method
Called foo method

(as well as an unchecked warning).

I know the match always succeeds due to type erasure, but that shouldn’t cause the problem, since the run-time type (even considering erasure) of f should be $anon$NameOfSomeAnonymousClassThatHasAfooMethod

When entered into the Scala REPL (2.9.1), it actually throws NoSuchMethodException:

<console>:11: warning: refinement AnyRef{def foo: Unit} in type pattern AnyRef{def foo: Unit} is unchecked since it is eliminated by erasure
              case f : {def foo : Unit} ⇒
                       ^
Has foo method
java.lang.NoSuchMethodException: $anon$1.foo()
        at java.lang.Class.getMethod(Class.java:1622)
        at .reflMethod$Method1(<console>:13)
        at .<init>(<console>:13)
        at .<clinit>(<console>:13)
        at .<init>(<console>:11)
        at .<clinit>(<console>)
        at $print(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
        at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
        at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
        at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
        at java.lang.Thread.run(Thread.java:679)

Why?

Edit

It turns out the proximate cause is that foo is generated as private. I speculated about the cause of this in my answer, but I’m not sure. If you have an idea, still feel free to post it as an answer!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:18:46+00:00Added an answer on June 8, 2026 at 11:18 am

    Upon more investigation, I find that the method was somehow made private:

    (new {
        def foo : Unit = println("Called foo method")
    } : Any) match {
        case f : {def foo : Unit} ⇒
            println("Has foo method")
            f.getClass.getDeclaredMethods
    }
    

    prints res5: Array[java.lang.reflect.Method] = Array(private void $anon$1.foo()).

    This is odd because Scala methods are supposed to be public by default.

    As Edmondo1984 points out, it works (foo method is public) if you remove the : Any.

    Speculatively

    I suspect the cause is that the compiler wrongly assumes that since the class is anonymous and the instance is declared to be of another type, its defined methods are uncallable from outside the class. This assumption would be valid in Java, but not in a language that provides structural typing. It therefore generates them as private, in an overzealous application of the principle of information hiding. If so, this is either a compiler bug or a language design corner case (using anonymous functions together with structural typing).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See this code: TicketStoreService fakeTicketStoreService = MockRepository.GenerateMock<TicketStoreService>(); fakeTicketStoreService.Expect(service => service.DoSomething(Arg.Is(new Guid())) .Return(new Guid()); fakeTicketStoreService.DoSomething(Arg.Is(new
This code gives me an empty result. I expect it to print out the
what behaviour can I expect when I run this code: do while(testA) { //
My expect code does this: It does a ssh connect to another machine, sends
Boy, this one is really weird. I expect the following code to print 1990,
I know that within constructor of Base class - when calling virtual method -
Using Java, I have a method which I'm calling with method.invoke that returns a
I expect this FQL query to return a non-empty array, because there are comments
I expect this might get some downvotes / closevotes but I'm going to ask
I'd expect this to be fairly routine, but cannot find a simple approach for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.