Hacker News new | ask | show | jobs
by morenoh149 4391 days ago
dude this is so cool. So scala can compile to the jvm right?
2 comments

It'll compile to JVM bytecode. I believe it also compiles to Javascript (I'm not sure if this is still considered experimental — I haven't looked in a while), which is pretty cool. I think it used to also target the .NET CLI as an experiment, but I'm pretty sure that's no longer a goal.

The important thing for me was that it compiles to JVM bytecode that's compatible with Java 1.6, so the resulting code can all be converted to Dalvik fairly quickly. The main downside right now is that you have to heavily prune the Scala stdlib, since it's packed with tons of methods and dex has a 16-bit method limit[1] — and you will hit it if you don't prune it (using proguard or another tool of your choice).

[1]: https://code.google.com/p/android/issues/detail?id=7147#c6

On Android, Scala gets compiled to dex bytecode just the same as Java. Scala has slightly more overhead though from making lots of small objects, but the tradeoff is worth it if you prefer not using Java.

I stick to using Java Collections (and the special ones Android provides[1]) most of the time to avoid excess overhead, but it still results in much more concise code than what I would write in Java. One of most compelling reasons to use Scala though is it's much easier to deal with asynchronous processing (via actors, futures, async/await[2]) than the messes you end up with using AsyncTask + callbacks with Java.

[1] http://developer.android.com/reference/android/support/v4/ut...

[2] http://docs.scala-lang.org/sips/pending/async.html

> Scala has slightly more overhead though from making lots of small objects

This is definitely true, though it's possible to write code that avoids it. It just ends up being slightly uglier Scala. The renderer in Ascension, for example, is all fairly bare-bones Java-like Scala because I have to be careful about how certain code is generated (i.e., prefer a while loop or tail recursion to a for comprehension or .foreach). So, it's possible to get performant code, it just ends up being more like Java usually.

In the UI code, you can get away with a little more depending on where it is. If it's inside an adapter, it pays to avoid the heavier tools Scala gives you. If it's responding to a button press, you probably don't need to worry as much. The GC will run a little more often, but the GC in Android has improved a fair bit over time.

I'll second the Java collections recommendation though — as much as I like the immutable collections provided by Scala (and I do use a few of them), they're much free-er with their allocations than Java and Android's. I haven't tried anything with actors on Android yet, though, so I can't comment on that. I just use AsyncTask as carefully as possible right now, but I really need to get familiar with an alternative to that. I'd like to try actors, plus I'd also like to get into using something like RxJava/Scala, but I haven't had anything come up where I could afford to experiment with it. Also need to see how Akka fares on Android, but same problem as RxJ/S.