
How Scala Compiles to Java - fogus
http://iamchrislewis.tumblr.com/post/239967776/a-look-at-how-scala-compiles-to-java
======
pacoverdi
Interesting post.

Another post that I'd like to read would be: How Many Objects Does Scala
Allocate (while executing some random code).

In my Java shop, we try to write ultra-efficient code. Avoiding the creation
of numerous small objects in critical sections is one of the best ways to
achieve that.

I'd like to start a couple projects in scala or clojure but I'm afraid that
the simplicity and expressiveness that these languages bring come at the cost
of performance (or at least on control on what the JVM does).

~~~
cabalamat
> _In my Java shop, we try to write ultra-efficient code._

Are you sure you should be doing that? As Knuth said, "premature optimization
is the root of all evil".

~~~
njm
I would suspect the poster has already weighed the cost vs. benefit of writing
efficient code. Many domains do require careful attention to performance.

------
paulitex
Very cool, but as someone unfamiliar with scala (and familiar with java) I
would really have liked to see java code for those 6 class files. Obviously
they would have to have been approximations, but seeing the 6 raw java source
files vs. the 1 scala (and the same .class outputs) I feel would have made a
very nice comparison. It also would do away with any ambiguity in the verbal
description of the congruencies between the two.

~~~
jorgeortiz85
This is approximately how the first code block in the OP would look like in
Java 5:

    
    
        import scala.Function1;
        import scala.List;
        import scala.runtime.RichInt;
    
        class App$ {
            public static final App$ MODULE$ = new App$();
        
            public boolean isEven(int i) {
                return i % 2 == 0;
            }
        
            public boolean isOdd(int i) {
                return i % 2 == 1;
            }
        
            public void main(String[] args) {
                final List<Integer> n = (new RichInt(1)).to(10).toList();
                n.filter(new Function1<Integer, Boolean>() {
                    public Boolean apply(final Integer x) {
                        return isEven(x);
                    }
                }).flatMap(new Function1<Integer, List<Integer>>() {
                    public List<Integer> apply(final Integer i) {
                        return n.filter(new Function1<Integer, Boolean>() {
                            public Boolean apply(final Integer x) {
                                return isOdd(x);
                            }
                        }).map(new Function1<Integer, Integer>() {
                            public Integer apply(final Integer j) {
                                return i * j;
                            }
                        });
                    }
                });
            }
        }
    
        class App {
            public static boolean isEven(int i) {
                return App$.MODULE$.isEven(i);
            }
        
            public static boolean isOdd(int i) {
                return App$.MODULE$.isOdd(i);
            }
        
            public static void main(String[] args) {
                App$.MODULE$.main(args);
            }
        }
    

Note that, although anonymous inner classes are valid syntax in Java 5, even
in Java 5 they are compiled down to separate class files. Hence 2 classes + 4
anonymous inner classes = 6 class files total.

(The code is only approximate. I haven't tried to compile it, so apologies for
any errors.)

