Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-1247

Passing a Function0 invocation to a call by name argument shouldn't create a new Function0

    Details

    • Type: Improvement
    • Status: CLOSED
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: Scala 2.10.0-M3
    • Component/s: Compiler (Misc)
    • Labels:
      None

      Description

      class Foo{
        val bar = () => "Hello world";
       
        def foo(x : =>String) = println(x);
       
        def baz = foo(bar());
      }

      generates the following bytecode for baz:

      public void baz();
        Code:
         0:	aload_0
         1:	new	SI-24; //class Foo$$$$anonfun$$baz$$1
         4:	dup
         5:	aload_0
         6:	invokespecial	SI-25; //Method Foo$$$$anonfun$$baz$$1."<init>":(LFoo;)V
         9:	invokevirtual	SI-29; //Method foo:(Lscala/Function0;)V
         12:	return

      So it's wrapping the call to bar() in an additional function, which ends up being () => bar().

      It would be nice if it didn't do this - it's not a huge deal, but it would be useful for a few cases where you want to use a call by name argument in an inner loop and this lets you hoist the Function0 passed to it out. It also means slightly fewer class files generated. It probably doesn't really matter though, so I'll mark this as minor.

      Note that it does do the right thing already if you pass a call by name argument to another call by name argument, so hopefully this is easy to do.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                odersky Martin Odersky
                Reporter:
                drmaciver David R. MacIver
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: