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 739129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:21:53+00:00 2026-05-14T08:21:53+00:00

I’m currently looking at closure implementations in different languages. When it comes to Scala,

  • 0

I’m currently looking at closure implementations in different languages. When it comes to Scala, however, I’m unable to find any documentation on how a closure is mapped to Java objects.

It is well documented that Scala functions are mapped to FunctionN objects. I assume that the reference to the free variable of the closure must be stored somewhere in that function object (as it is done in C++0x, e.g.).

I also tried compiling the following with scalac and then decompiling the class files with JD:

object ClosureExample extends Application { 
  def addN(n: Int) = (a: Int) => a + n
  var add5 = addN(5)
  println(add5(20))
}

In the decompiled sources, I see an anonymous subtype of Function1, which ought to be my closure. But the apply() method is empty, and the anonymous class has no fields (which could potentially store the closure variables). I suppose the decompiler didn’t manage to get the interesting part out of the class files…

Now to the questions:

  • Do you know how the transformation is done exactly?
  • Do you know where it is documented?
  • Do you have another idea how I could solve the mystery?
  • 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-05-14T08:21:53+00:00Added an answer on May 14, 2026 at 8:21 am

    Let’s take apart a set of examples so we can see how they differ. (If using RC1, compile with -no-specialization to keep things easier to understand.)

    class Close {
      var n = 5
      def method(i: Int) = i+n
      def function = (i: Int) => i+5
      def closure = (i: Int) => i+n
      def mixed(m: Int) = (i: Int) => i+m
    }
    

    First, let’s see what method does:

    public int method(int);
      Code:
       0:   iload_1
       1:   aload_0
       2:   invokevirtual   #17; //Method n:()I
       5:   iadd
       6:   ireturn
    

    Pretty straightforward. It’s a method. Load the parameter, invoke the getter for n, add, return. Looks just like Java.

    How about function? It doesn’t actually close any data, but it is an anonymous function (called Close$$anonfun$function$1). If we ignore any specialization, the constructor and apply are of most interest:

    public scala.Function1 function();
      Code:
       0:   new #34; //class Close$$anonfun$function$1
       3:   dup
       4:   aload_0
       5:   invokespecial   #35; //Method Close$$anonfun$function$1."<init>":(LClose;)V
       8:   areturn
    
    public Close$$anonfun$function$1(Close);
      Code:
       0:   aload_0
       1:   invokespecial   #43; //Method scala/runtime/AbstractFunction1."<init>":()V
       4:   return
    
    public final java.lang.Object apply(java.lang.Object);
      Code:
       0:   aload_0
       1:   aload_1
       2:   invokestatic    #26; //Method scala/runtime/BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I
       5:   invokevirtual   #28; //Method apply:(I)I
       8:   invokestatic    #32; //Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
       11:  areturn
    
    public final int apply(int);
      Code:
       0:   iload_1
       1:   iconst_5
       2:   iadd
       3:   ireturn
    

    So, you load a “this” pointer and create a new object that takes the enclosing class as its argument. This is standard for any inner class, really. The function doesn’t need to do anything with the outer class so it just calls the super’s constructor. Then, when calling apply, you do the box/unbox tricks and then call the actual math–that is, just add 5.

    But what if we use a closure of the variable inside Close? Setup is exactly the same, but now the constructor Close$$anonfun$closure$1 looks like this:

    public Close$$anonfun$closure$1(Close);
      Code:
       0:   aload_1
       1:   ifnonnull   12
       4:   new #48; //class java/lang/NullPointerException
       7:   dup
       8:   invokespecial   #50; //Method java/lang/NullPointerException."<init>":()V
       11:  athrow
       12:  aload_0
       13:  aload_1
       14:  putfield    #18; //Field $outer:LClose;
       17:  aload_0
       18:  invokespecial   #53; //Method scala/runtime/AbstractFunction1."<init>":()V
       21:  return
    

    That is, it checks to make sure that the input is non-null (i.e. the outer class is non-null) and saves it in a field. Now when it comes time to apply it, after the boxing/unboxing wrapper:

    public final int apply(int);
      Code:
       0:   iload_1
       1:   aload_0
       2:   getfield    #18; //Field $outer:LClose;
       5:   invokevirtual   #24; //Method Close.n:()I
       8:   iadd
       9:   ireturn
    

    you see that it uses that field to refer to the parent class, and invokes the getter for n. Add, return, done. So, closures are easy enough: the anonymous function constructor just saves the enclosing class in a private field.

    Now, what about if we close not an internal variable, but a method argument? That’s what Close$$anonfun$mixed$1 does. First, look at what the mixed method does:

    public scala.Function1 mixed(int);
      Code:
       0:   new #39; //class Close$$anonfun$mixed$1
       3:   dup
       4:   aload_0
       5:   iload_1
       6:   invokespecial   #42; //Method Close$$anonfun$mixed$1."<init>":(LClose;I)V
       9:   areturn
    

    It loads the parameter m before calling the constructor! So it’s no surprise that the constructor looks like this:

    public Close$$anonfun$mixed$1(Close, int);
      Code:
       0:   aload_0
       1:   iload_2
       2:   putfield    #18; //Field m$1:I
       5:   aload_0
       6:   invokespecial   #43; //Method scala/runtime/AbstractFunction1."<init>":()V
       9:   return
    

    where that parameter is saved in a private field. No reference to the outer class is kept because we don’t need it. And you ought not be surprised by apply either:

    public final int apply(int);
      Code:
       0:   iload_1
       1:   aload_0
       2:   getfield    #18; //Field m$1:I
       5:   iadd
       6:   ireturn
    

    Yes, we just load that stored field and do our math.

    I’m not sure what you were doing to not see this with your example–objects are a little tricky because they have both MyObject and MyObject$ classes and the methods get split between the two in a way that may not be intuitive. But apply definitely applies things, and overall the whole system works pretty much the way you’d expect it to (after you sit down and think about it really hard for a really long time).

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

Sidebar

Ask A Question

Stats

  • Questions 411k
  • Answers 411k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In your callback, the data parameter is indeed a CFDataRef… May 15, 2026 at 7:42 am
  • Editorial Team
    Editorial Team added an answer Function template partial specialization is not allowed. Do template <int… May 15, 2026 at 7:42 am
  • Editorial Team
    Editorial Team added an answer Should be ${"myArray"} or if the string is contained in… May 15, 2026 at 7:42 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.