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

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

Closed
scabug opened this Issue Aug 20, 2008 · 4 comments

Comments

Projects
None yet
2 participants
@scabug
Copy link

scabug commented Aug 20, 2008

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.

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Aug 20, 2008

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Aug 20, 2008

@DRMacIver said:
Ok, I won't mark this as minor because I can't set priorities. But please feel free to. :-)

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Aug 20, 2008

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

@scabug

This comment has been minimized.

Copy link
Author

scabug commented May 12, 2012

@retronym said:
Fixed by Johannes Rudolph scala/scala@00a648b

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