18 ms·
Java to Scala converter
- ensmotko 13y agoWow, this is awesome. Have you tried running it on a larger Java codebase?
- lukax 13y agoThis online tool was created to convert shorter Java code samples but larger files also work (e.g. https://github.com/sirthias/pegdown/blob/master/src/main/java/org/pegdown/Parser.java https://github.com/sirthias/pegdown/blob/master/src/main/jav...)
- adandy 13y agoI also noticed this in IntelliJ when I went to copy some Java into a Scala project. As I began converting the Java code to Scala after the copy I was extremely confused when I found that the pasted code was already converted for me.
- Xorlev 13y agoThat being said, it doesn't work very well in many circumstances. I'm not sure if they're using Scalagen under the hood or their own home-baked stuff. It's impressive as-is, regardless of the issues.
- agibsonccc 13y agoThis is a neat project. I have a giant codebase I'd like to try this on. Depending on the output, this might be the kick in the pants I need to get in to scala full time.
- eip 13y agoYou can use Java and Scala in the same project.
- agibsonccc 13y agoI'm plenty aware. I just want to migrate my whole codebase to scala. I like the JVM itself, but would like some of the features of scala. My main point is I've been procrastinating, but this could be a great motivator. 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.
- lmm 13y agoScala that's been autoconverted from Java doesn't tend to be very idiomatic scala, and there's no way to convert back. I'd recommend writing bits of your code (particularly code that needs to convert between datastructures, or code where you need better control over effects) in scala first and see how you get on with it.
- agibsonccc 13y agoYeah I looked in to the differences after I played with it a bit..generated code is never ideal in the first place though. 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.
- zidar 13y agoThis looks great. Now if only Scala would play nice with Android, there would be no need to look back at java at all.
- edofic 13y agoI made a few android apps with scala. What's not working for you?
- anonymoushn 13y agoenums are converted into code that does not compile.
- gtani 13y agoI think you would have to check converted code for all the well documented interop issues: annotations, static members, checked exceptions etc. The Manning books "In Depth" and "In Action" cover these really well,
- mcav 13y agoCool, has anyone made something similar for Clojure?
- zindlerb 13y agoIsn't the whole idea of clojure that it is built on JVM?
- tel 13y agoI'd say it's an somewhat important side point... But Clojure already cross-compiles to Javascript.
- sreque 13y agoThere have been a number of not-quite-successful attempts in the past to compile Scala to Javascript. The latest and most promising seems to be https://github.com/lampepfl/scala-js https://github.com/lampepfl/scala-js
- lmm 13y agoSo is scala, and scala already interoperates with java code. But it's still nice to be able to turn your legacy java code into scala and have all your code in a single language.
- cynicalkane 13y agoUnlike Scala, having Java-style code in Clojure yields no advantages. You just end up with something like Java, but with less type safety and more parentheses. It also makes a lousy start for refactoring since effective Clojure is so much about figuring out how to shape your code into nested S-Expressions, so you'll end up nuking most of your auto-converted Java "statements".
- elangoc 13y agoClojure doesn't lend itself to doing that. Perhaps the easiest (albeit simplistic) way to describe the difference between Clojure and Scala -- Scala supports OOP and FP, and Clojure supports FP only as part of a holistic (and refreshing) vision of how high-level programming is done. 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.
- scottjad 13y agoThis is just a simple front-end/demo. The real work is being done by a separate project at https://github.com/mysema/scalagen https://github.com/mysema/scalagen. I kind of wished this site was written in Java and run through scalagen with the output being run as an additional meta-demonstration.
- dkhenry 13y agoSo now I am really wondering. What is the sub java parts of the JVM that need to be implemented to completely free Scala from the JVM ( i.e. what C interfaces are needed to allow scalc to compile to LLVM or comparable VM)
- lmm 13y agoWhy would you want to? The JVM is pretty awesome. There's already a scala implementation for .net, so it's got at least some level of VM independence, but the JVM is the primary target and I'd expect it to remain so. 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.
- dkhenry 13y agoSpeed and size would be the two biggest. 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 )
- pjmlp 13y agoIt is a matter of using the right JVM for the job. Many do allow for AOT compilation, or caching the generated JIT code between invocations. IBM J9 is one of them, for example.
- TylerE 13y agoMy understanding is that the bulk of JVM startup time is NOT compiling or loading the actual application code, but loading the classpath.
- pjmlp 13y agoAs I said, not all JVMs are made alike, don't confuse Java virtual machines, with Oracle's or OpenJDK implementation. JVMs that cache JITed code don't spend time loading the classpath if the cache is still valid.
- ben0x539 13y agoI don't know scala so maybe I'm missing something, but does this just break with functions that start with a switch statement? eg. 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 :)
- tommorris 13y agoYes. The problem with the switch is that in Scala, 'match' actually requires you to have at least one pattern matching rule. 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.
- ben0x539 13y agoSorry, maybe I minimized my example too eagerly. 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.
- unclebucknasty 13y agoEarnest question: who is the target audience here? If Java and Scala can be intermixed in the JVM, and the generated Scala code is not very idiomatic, then how would this be used?
- lukax 13y agoJust a few examples: 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.
- mark_eijsermans 13y agoNow we just need a OOP -> FP converter ;)
- zeckalpha 13y agoOr just use both at what they are best at.
- cgag 13y agoUnfortunately most people seem to think OO happens to be best at the things they're already used to using OO with for historical reason.
- seanmcdirmid 13y agoUnfortunately, many people seem to think FP is the hot new thing and OOP is already over; they ignore that OOP happens to be a great way to structure code that focuses on naturalism, while FP forces math-like structuring. There is massive fad-like interest in FP right now, especially in academia.
- zeckalpha 13y agoAnd vice versa.
- seanmcdirmid 13y agoI don't see the vice versa in academia at all. Where is this OOP hype that you think exists? Even OOPSLA changed its name to SPLASH to de-emphasize objects.
- zeckalpha 13y agoYou said especially in academia. I just meant that OOP is fad-like, too.
- ExpiredLink 13y agoA Scala to Java converter would be needed more. Early adopters are disenchanted with Scala.
- runT1ME 13y agoI don't think this is true at all. There was the one team at Yammer, but overall, it seems as if Netflix and LinkedIn are pushing forward with Scala adoption, and Twitter, FourSquare, Tumblr, Klout, Gravity, and many others are already committed scala shops.
- eranation 13y agoAs an early adopter, I don't feel disenchanted. Do you have references to back your claim?
- mark242 13y agoThis is completely untrue. 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.
- yareally 13y agoIntellij IDEA has had Java to Scala conversion built into it for a while (as well as Java to Kotlin, Jetbrain's new JVM Scala-like language). I'm not sure how Jetbrain's tool compares to the one in the link above, but the conversion result in Intellij varies by how complicated and dependent the code is on other packages/libraries. Converting an entire class file tends to work out much better than a single method. 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."
- orph 13y agoWould love the opposite, a scala -> java converter.