Skip to content

reduce duplication of FunctionN as scala.runtime.java8.JFunction #109

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
adriaanm opened this issue Mar 26, 2016 · 4 comments
Closed

reduce duplication of FunctionN as scala.runtime.java8.JFunction #109

adriaanm opened this issue Mar 26, 2016 · 4 comments

Comments

@adriaanm
Copy link
Contributor

We'll still need something for void-return functions. First stab: scala/scala@0db3f41

@retronym
Copy link
Member

The JProcedureN classes aren't actually used by the current backend, we create an "$adapted" method that converts void to BoxedUnit.

The difficulty is with specialization.

import scala.*;
import java.util.Arrays;

public class Test {
    private static void printApplyMethods() {
         Arrays.stream(Thread.currentThread().getStackTrace()).filter(x -> x.getMethodName().startsWith("apply")).forEach(System.out::println);
         System.out.println();
    }
    public static void main(String[] args) {
        Function0<String> f0_String = () -> "";
        f0_String.apply();
        Function0<? extends Object> f0_Object = f0_String;
        f0_Object.apply();

        Function0$mcI$sp f0_int = () -> {printApplyMethods(); return 42;};
        f0_int.apply();
        f0_int.apply$mcI$sp(); // Routes through boxing in `scala.Function0.apply$mcI$sp`
        Function0<Integer> f0_Integer = (Function0<Integer>) (Object) f0_int;
        f0_Integer.apply();

        Function1<String, String> f1_String_String = x -> x;
        f1_String_String.apply("");
        Function1$mcII$sp f1_int_int = x -> {printApplyMethods(); return 42;};
        f1_int_int.apply$mcII$sp(0); // Routes through boxing in `scala.Function1.apply$mcII$sp`
        Function1<Integer, Integer> f1_Integer_Integer = (Function1<Integer, Integer>) (Object) f1_int_int;
        f1_Integer_Integer.apply(42);
    }
}
javac -d . -classpath build/quick/classes/library sandbox/Test.java && qscala -J-XX:+UnlockDiagnosticVMOptions -J-XX:+ShowHiddenFrames Test
Note: sandbox/Test.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Test$$Lambda$125/1982791261.apply(<Unknown>:1000000)
scala.reflect.internal.util.ScalaClassLoader$$Lambda$123/1993134103.apply(<Unknown>:1000008)

Test$$Lambda$125/1982791261.apply(<Unknown>:1000000)
scala.Function0.apply$mcI$sp(Function0.scala:34)
scala.reflect.internal.util.ScalaClassLoader$$Lambda$123/1993134103.apply(<Unknown>:1000008)

Test$$Lambda$125/1982791261.apply(<Unknown>:1000000)
scala.reflect.internal.util.ScalaClassLoader$$Lambda$123/1993134103.apply(<Unknown>:1000008)

Test$$Lambda$129/1763847188.apply(<Unknown>:1000001)
scala.Function1.apply$mcII$sp(Function1.scala:36)
scala.reflect.internal.util.ScalaClassLoader$$Lambda$123/1993134103.apply(<Unknown>:1000008)

Test$$Lambda$129/1763847188.apply(<Unknown>:1000001)
scala.reflect.internal.util.ScalaClassLoader$$Lambda$123/1993134103.apply(<Unknown>:1000008)

An easy first step would be to change non-specialized lambdas to directly use FunctionN.

@retronym
Copy link
Member

Related: #36

@adriaanm
Copy link
Contributor Author

Right you are. I misinterpreted the crash.

@retronym
Copy link
Member

This is largely fixed in scala/scala#4971. I'm going to close this and open a followup (low priority) ticket for specialized FunctionN.

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

No branches or pull requests

2 participants