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 Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: Scala 2.10.0-M3
    • Component/s: Misc Compiler
    • 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.

        Issue Links

          Activity

          Hide
          David R. MacIver added a comment -

          Ok, I won't mark this as minor because I can't set priorities. But please feel free to.

          Show
          David R. MacIver added a comment - Ok, I won't mark this as minor because I can't set priorities. But please feel free to.
          Hide
          Iulian Dragos added a comment -

          These types are regarded as different in the compiler. Maybe Martin can comment on it.

          Show
          Iulian Dragos added a comment - These types are regarded as different in the compiler. Maybe Martin can comment on it.
          Show
          Jason Zaugg added a comment - Fixed by Johannes Rudolph https://github.com/scala/scala/commit/00a648bc909f794d91570fb962fc7d7fcd055bc5

            People

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

              Dates

              • Created:
                Updated:
                Resolved:

                Development