Code Monkey home page Code Monkey logo

Comments (3)

philliptaylorpro avatar philliptaylorpro commented on July 17, 2024 1

Hi, I don't think you should add this rule. If two identical pieces of code result in different bytecode then it feels like a bug or missing feature in the compiler. As you soon as you start saying "this way = bad, this way = right" then you turn the Scala community into sheep that follow rules without understanding why, or your force every new person to become a compiler expert before they can be confident in their code. Developers be shouldn't concerned with compiler rules and vice versa, you don't want to make people do things one way only for the Scala compilers to fix it and make it bad advice that's shaped your app into something less maintainable.

I feel the same way about the "final" modifier missing on case object. I've not done something wrong, or slow and you should fix the compiler.

Anyway...just some nobodies opinion.

from scapegoat.

sksamuel avatar sksamuel commented on July 17, 2024

I need a justification for this. Is

b match { 
case true =>
case false =>
}

slower than

if (b) {} else {}

It feels like it should be.

from scapegoat.

lespea avatar lespea commented on July 17, 2024

TLDR: Yup it's a more efficient writing if/else

With no flags passed to the compiler:

public int testIfElse(boolean);
    descriptor: (Z)I
    Code:
    0: iload_1
    1: ifeq          8
    4: iconst_3
    5: goto          9
    8: iconst_4
    9: ireturn

public int testMatchWithWildcard(boolean);
    descriptor: (Z)I
    Code:
    0: iload_1
    1: istore_2
    2: iconst_1
    3: iload_2
    4: if_icmpne     12
    7: iconst_3
    8: istore_3
    9: goto          14
    12: iconst_4
    13: istore_3
    14: iload_3
    15: ireturn

public int testMatchWithoutWildcard(boolean);
    descriptor: (Z)I
    Code:
    0: iload_1
    1: istore_2
    2: iconst_1
    3: iload_2
    4: if_icmpne     12
    7: iconst_3
    8: istore_3
    9: goto          19
    12: iconst_0
    13: iload_2
    14: if_icmpne     21
    17: iconst_4
    18: istore_3
    19: iload_3
    20: ireturn
    21: new           #21                 // class scala/MatchError
    24: dup
    25: iload_2
    26: invokestatic  #27                 // Method scala/runtime/BoxesRunTime.boxToBoolean:(Z)Ljava/lang/Boolean;
    29: invokespecial #30                 // Method scala/MatchError."<init>":(Ljava/lang/Object;)V
    32: athrow

With a lot of optimizing flags passed to the compiler (-optimize & the new genbcode + friends):

public int testIfElse(boolean);
    descriptor: (Z)I
    Code:
    0: iload_1
    1: ifeq          8
    4: iconst_3
    5: goto          9
    8: iconst_4
    9: ireturn

public int testMatchWithWildcard(boolean);
    descriptor: (Z)I
    Code:
    0: iconst_1
    1: iload_1
    2: if_icmpne     10
    5: iconst_3
    6: istore_2
    7: goto          12
    10: iconst_4
    11: istore_2
    12: iload_2
    13: ireturn

public int testMatchWithoutWildcard(boolean);
    descriptor: (Z)I
    Code:
    0: iconst_1
    1: iload_1
    2: if_icmpne     10
    5: iconst_3
    6: istore_2
    7: goto          17
    10: iconst_0
    11: iload_1
    12: if_icmpne     19
    15: iconst_4
    16: istore_2
    17: iload_2
    18: ireturn
    19: new           #21                 // class scala/MatchError
    22: dup
    23: iload_1
    24: invokestatic  #27                 // Method scala/runtime/BoxesRunTime.boxToBoolean:(Z)Ljava/lang/Boolean;
    27: invokespecial #30                 // Method scala/MatchError."<init>":(Ljava/lang/Object;)V
    30: athrow

from scapegoat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.