110 points · by lukax · 13y ago · 87 comments
I kind of wished this site was written in Java and run through scalagen with the output being run as an additional meta-demonstration.
public static void main(String[] args) {
switch (42) {}
System.out.println("Hello World!");
}
converts to def main(args: Array[String]) 42 match {
}
dropping the println.Edit: It also seems to be dropping autoincrement/decrement, and random other blocks of code I've looked at, so I guess it's still in a fairly early stage.
Definitely a cool project though :)
The closest I'd wrote in Scala to your example is something like this:
def main(args: Array[String]) = {
42 match {case i: Any => None}
println("Hello World!")
}
Ideally, the switch (42) {} should be removed as it's not doing anything. class HelloWorldApp {
public static void main(String[] args) {
switch (args.length) {
case 0: System.out.println("No args!"); break;
case 1: System.out.println("One arg!"); break;
default: System.out.println("Multiple args!"); break;
}
System.out.println("Hello World!");
}
}
This also seems to not emit the "Hello World" println.1. Someone still learning Scala syntax (and already knows Java)
2. Let's say you already have an existing Scala code base and you want to try out some new Java library which has examples in Java but you want to use it in your Scala code.
Every single Java developer that I introduce to Scala-- every single one-- six months later, they tell me "please don't make me go back to Java".
The "disenchanted" piece, I think, comes from the fact that if you're just writing Java-OO-style in Scala, you're losing almost all the benefits. Once you start working with the FP style, understanding Options, working with fold, list comprehensions, and the resulting massive reduction in lines of code, you start seeing how FP Scala code becomes the last refactoring you'll ever need to do, because it's so concise.
Given the skills we get on some of our teams, I fear they won't even manage to move to Java 8!
Since everyone else seems to disagree, I'll explain why I agree:
-- Scala advocates a completely different style of programming. You have to rethink how you code to match Scala's functional style. This is not a trivial thing if you have a bunch of Java developers you need to convert.
-- I find the Scala documentation is less readable than Java's docs. They are cluttered with type conversion functions and unreadable operator overloads (the filtering helps a bit here, but it's extra work). Documentation is also split between the object/class/trait pages.
-- There's a whole class of immutable data structures whose performance characteristics I need to learn, since I'm supposed to be using them.
-- I'm not convinced that the overhead of copying immutable data doesn't outweigh the benefits of the ability to trivially parallelize the code.
-- I'm not convinced that a project preferring only immutable data is easier to maintain.
-- Programs seem to take longer to compile (sbt is especially slow).
Now that I think about it, Scala in relation to Java is a lot like C++ in relation to C. Scala sits on top of Java, so you need to know Java, and then you need to know all of Scala's features as well. However, (as someone who knows Java) I think C++ adds useful abstractions that are non-existant in C (templating, inheritance), whereas most of Scala's features are just syntactical changes of what already exists in Java.
Let me introduce you to my friend, Mister ConcurrentModificationException. He's a wily fellow-- he rarely pops up when you're doing local development, but he has this nasty habit of showing up in production whenever you start hitting a decent load. He's like Keyser Soze. You see him, then you try to debug the place he shows up, and poof he's gone, back into the shadows from whence he came, only to reappear when you try to iterate over that singleton java.util.ArrayList once more.
Now, let me introduce you to my other friend, Miss scala.collection.immutable.List. She's a superheroine. You can run code using her on one thread, or ten threads, or ten thousand threads, she doesn't care, you're always going to get the same iterator back when you call List.foreach. For the very small price of a little bit of garbage (which Mister ParallelGC takes care of very readily), Miss immutable.List will return that filtered list the exact same way, every single time you run the filter method with those parameters you set. You can run code with her in development, staging, production, she's always ready to go, and you'll never run into Mister ConcurrentModificationException when you're dealing with Miss immutable.List.
-- I'm not convinced that a project
What do you mean? All I read was "I'm not convinced that a project". Someone else must have come by and edited your post before I was able to read it.
You do realize that concurrency and parallelization are very different topics right? One is a necessary property, the other is one of oppurtunity. It makes sense that collections designed for concurrency would suck at parallelism.
So...don't use data structures that are not thread-safe in threaded environments? Wrap it with Collections.synchronizedCollection. Or make copies, if you prefer.
For the very small price of a little bit of garbage...
What small price? If I have millions of things stored in an immutable data structure, do I not have to copy the entire structure (or at least some portion of it) when I want to modify anything inside of it?
Thanks for reminding me of him. I have never seen him during my last five years of programming in Java. I hope he's alive and well.
That's not true. Scala sits on the JVM, and has some accommodations to facilitate interoperation with code written in Java, but it does not "sit on top of Java" as a language. You don't need to know Java to write Scala.
>You don't need to know Java to write Scala
Is this actually true in practice? Are there tutorials or books that don't assume knowledge of Java? In any case, if you want to use anything in the Java standard library (I assume they haven't converted the entire thing to Scala), then you'll still need to understand Java documentation which requires some knowledge of Java.
> -- Scala advocates a completely different style of programming. You have to rethink how you code to match Scala's functional style. This is not a trivial thing if you have a bunch of Java developers you need to convert.
Imperative Scala is both performant and readable. For the application I work on, in many places it is the only style that is sufficiently performant.
> -- I find the Scala documentation is less readable than Java's docs. They are cluttered with type conversion functions and unreadable operator overloads (the filtering helps a bit here, but it's extra work). Documentation is also split between the object/class/trait pages.
This is pretty much true. Good IDE support and #scala@freenode help.
> -- There's a whole class of immutable data structures whose performance characteristics I need to learn, since I'm supposed to be using them.
You can use whatever data structures you want, including the ones in java.util. If immutable data structures are appropriate for your application (for instance, because you want nondeterminism based on Scala's delimited continuations, and repeated invocations of the same continuation should of course see the same data, or because you want to pass a lasting view of the current state of your collection to another thread), then you can use them.
> -- I'm not convinced that the overhead of copying immutable data doesn't outweigh the benefits of the ability to trivially parallelize the code.
This is an objection to a particular way of structuring applications, rather than to a language.
> -- I'm not convinced that a project preferring only immutable data is easier to maintain.
This is also not a Scala thing. My insignificant industry experience indicates that it is true. More importantly, John Carmack seems to think so: https://news.ycombinator.com/item?id=6278047
> -- Programs seem to take longer to compile (sbt is especially slow).
Yeah. I would be okay with this if I got the GLORIOUS TYPE SAFETY from it, but Scala does not really deliver on this front.
> then you need to know all of Scala's features as well.
This is a big burden, to be sure, and Scala introduces some unreasonable gotcha's along the way. Having already learned Scala, though, there's no reason I would sit down and write Java. Unless I wanted an enum.
I wanted to see how close it was..but the mapping just isn't what I was quite looking for. There really is no silver bullet though.
I've also worked with intermixing the different JVM languages including clojure, working with SBT, etc.
Good points have been raised that it wouldn't be idiomatic scala though.
I initially used it to convert some Android code in Java to Scala, but much of the time you have to end up rewriting that as well, since it's generally results in very non-idiomatic Scala code. Still useful for someone learning Scala though as others have pointed out.
The Scala plugin is free though and comes with the community edition of Intellij for anyone wanting to try it out. Basically you just copy/paste some Java code from the current project into a Scala file or right click on the Java file and select "convert."
As well as the VM there's the runtime library to consider. There was a project recently here that compiled scala to javascript to use on web pages, but unfortunately it needed a 16mb standard library.
Scala.net was abandoned a while ago. It's close to impossible to get it to run because .Net's generics are reified.
The start up time of the JVM and the size of the generated executables ( jar files ) make scala not terribly useful for those small programming tasks ( scripting and such )
Many do allow for AOT compilation, or caching the generated JIT code between invocations. IBM J9 is one of them, for example.
- compiler backend page (info about CLR: http://lampwww.epfl.ch/~magarcia/ScalaCompilerCornerReloaded...
- scala to JS (one of multiple projects): http://www.infoq.com/news/2013/06/scalajs
- akka is (i think) tightly bound to Oracle JVM/openJDK, https://groups.google.com/forum/#!msg/akka-user/0Z-9_Bmt8p4/...
Scala and the JVM are very tightly coupled - targeting LLVM or something should be possible, but getting that to run with high performance would be very difficult.
There is massive fad-like interest in FP right now, especially in academia.
Even if it were possible to do so in Clojure, you probably wouldn't want to. There are usually ways to do the same things in Clojure that makes your code simpler, and as a result, in much less code.
Part of the Clojure philosophy is to make things simpler, even if it isn't always 'easy' at first relative to the individual talking. Even though using a code translation tool is easy, figuring out how to use functions and macros to abstract away repetitious code blocks and patterns from your code is well worth it in the medium- and long-term.