Comments (3)
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.
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.
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)
- Enabling auto-merge
- false positive VariableShadowing when use copy in map HOT 1
- Please compile plugin for Scala 2.13.7 HOT 2
- New release for Scala 2.13.7 HOT 8
- Please compile plugin for Scala 2.13.8 HOT 3
- Support for 2.13.8 HOT 1
- New inspection level - SKIP HOT 3
- Issues when releasing 1.4.12 / 1.4.13 HOT 5
- Support for 2.13.9 HOT 2
- Scoverage failure HOT 4
- Add "hacktoberfest" topic HOT 1
- Please release 1.4.17 for scala 2.12.17 HOT 3
- New Inspection: Avoid `require`
- please compile for scala 2.13.11 HOT 2
- `UnnecessaryConversionTest` failing in latest Scala community build HOT 2
- mvn compile build failure InterfaceCompileFailed HOT 3
- Support Gitlab / Clodeclimate report format HOT 3
- Scala 2.13.12 HOT 1
- Support Actionable Diagnostics from Scala 2.13.12+ HOT 2
- Using Cons bypasses Option.Get violation HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scapegoat.