Skip to content

Passing a Function0 by-name generates an (unnecessary?) wrapper #14623

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

Closed
armanbilge opened this issue Mar 5, 2022 · 0 comments · Fixed by #14628
Closed

Passing a Function0 by-name generates an (unnecessary?) wrapper #14623

armanbilge opened this issue Mar 5, 2022 · 0 comments · Fixed by #14628
Assignees
Milestone

Comments

@armanbilge
Copy link
Contributor

Initially discussed in:

after being discovered in:

Additional background:

Minimized example

object Thunk {
  private[this] val impl =
    ((x: Any) => x).asInstanceOf[(=> Any) => Function0[Any]]

  def asFunction0[A](thunk: => A): Function0[A] = impl(thunk).asInstanceOf[Function0[A]]
}

"Thunk.asFunction0" should {
  "return the same function" in {
    var i = 0
    val f = () => i += 1
    Thunk.asFunction0(f()) eq f
  }
}

Output

3.0.2

 private static final boolean $init$$$anonfun$2$$anonfun$1() {
    IntRef i = IntRef.create(0);
    Function0 f = () -> {
       int var1 = i.elem + 1;
       i.elem = var1;
    };
    return cats.effect.Thunk..MODULE$.asFunction0(f) == f; // true
 }

3.1.3-RC1-bin-20220207-74c2954-NIGHTLY

Following changes in:

 private static final void $init$$$anonfun$1$$anonfun$1$$anonfun$1(final Function0 f$1) {
    f$1.apply$mcV$sp();
 }

 private static final boolean $init$$$anonfun$1$$anonfun$1() {
    IntRef i = IntRef.create(0);
    Function0 f = () -> {
       int var1 = i.elem + 1;
       i.elem = var1;
    };
    return cats.effect.Thunk..MODULE$.asFunction0(() -> {
       $init$$$anonfun$1$$anonfun$1$$anonfun$1(f);
       return BoxedUnit.UNIT;
    }) == f; // false
 }

Expectation

In 2.13.8, this optimization occurs without any ceremony on our part.

object Thunk {
  def asFunction0[A](thunk: => A): Function0[A] = () => thunk
}

"Thunk.asFunction0" should {
  "return the same function" in {
    var i = 0
    val f = () => i += 1
    Thunk.asFunction0(f()) eq f // true
  }
}
@armanbilge armanbilge added the stat:needs triage Every issue needs to have an "area" and "itype" label label Mar 5, 2022
odersky added a commit to dotty-staging/dotty that referenced this issue Mar 6, 2022
Generalize eta reduce so that it also handles eta expansions that are subject to
adaptation or specialization.

Fixes scala#14623
@odersky odersky added area:transform and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 6, 2022
jchyb pushed a commit to jchyb/dotty that referenced this issue Mar 8, 2022
Generalize eta reduce so that it also handles eta expansions that are subject to
adaptation or specialization.

Fixes scala#14623
@Kordyjan Kordyjan added this to the 3.1.3 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants