Skip to content

Consider use x.getClass as a null check #107

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
retronym opened this issue Mar 24, 2016 · 3 comments
Closed

Consider use x.getClass as a null check #107

retronym opened this issue Mar 24, 2016 · 3 comments

Comments

@retronym
Copy link
Member

These are semantically equivalent:

if (x eq null) throw new NullPointerException() //1
if (x eq null) throw null //2
x.getClass //3

We use option 2 in our null checks for the outer pointer in inner class constructors. I just noticed that javac used option 3 when spinning up a lambda based on an instance method reference.

    //  0: aload_0
    //  1: astore_1
    //  2: aload_1
    //  3: dup
    //  4: invokevirtual #3                  // Method java/lang/Object.getClass:()Ljava/lang/Class;
    //  7: pop
    //  8: invokedynamic #2,  0              // InvokeDynamic #0:x:(LTest;)LTest$FI1; (invokevirtual Test.bar:(Ljava/lang/Object;Ljava/lang/Object;)V)
    // 13: areturn
FI1 a1() { Test t1 = this; return t1::bar; }
@sjrd
Copy link
Member

sjrd commented Nov 9, 2016

That would be bad for performance in Scala.js. That said, it wouldn't be hard to optimize a call to getClass in statement position into a null check in our optimizer, if you choose to go that route.

@lrytz
Copy link
Member

lrytz commented Nov 9, 2016

we could just do that in our backend, so it wouldn't reach scala.js codegen

@dwijnand
Copy link
Member

Curious, but sounds like the status quo is good enough or better.

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

No branches or pull requests

4 participants