tags:

views:

181

answers:

2

I have been learning Scala for the past couple of months and now I feel I can start using into real work apart from solving some simple problems. My question here is how well do these two work together?

I have a couple of Java projects which I am working on now. How easy wiil it be to start using scala in them? Are there any gotchas to be aware of? Are there any tutorials or kind of stuff available on doing it? If I want to use Scala in web projects how to do it (other than lift)? All ideas and suggestions welcome.

+11  A: 

In general, using Java libraries from Scala code is easier than using Scala stuff from Java. That tends to happen because Scala has advanced features that are technically accessible in java, but implemented by the scala compiler by generating all kinds of extra classes with mangled names.

If your "main" program is going to be written in java for now, but you want to implement some functionality or modules in scala, you should probably define your modules in terms of Java interfaces. Then, have your scala code implment those interfaces.

Hopefully, you will soon find that you like scala so much that you switch to scala being the main language, calling into your old java code.

Peter Recore
I wholeheartedly agree with these sentiments: this is exactly how I've combined the two also. (I.e. either Scala main, or Java main interacting with Scala through Java interfaces in order to avoid the need for name-mangling in the Java code.)
Rex Kerr
grrrr, "name mangling" is the technical word I could not think of at the time i wrote this, resulting in my use of the term "funny names."
Peter Recore
The `@BeanProperty` annotation can be your friend in making vals and vars more accessible to java code. It generates java-style getFoo and setFoo that you can use from java code (though the scala compiler won't let you access those names from your scala code).
Ken Bloom
+1  A: 

In general, I don't get the premise of mixed-language projects except in extremely specialized cases. Scala is capable (as a language) of expressing everything Java can and more. So why bother "introducing Scala into a Java project"? Why not just write a pure-Scala project?

When I say pure Scala, I'm taking it as read that you may wish to reference a JAR file which was written in Java. But for the vast majority of cases, I see no need to write Java code in a Scala project.

oxbow_lakes
If you have a large application, it might not be practical to rewrite the entire thing just so it is pure scala, especially in the enterprise world. But you may want to use Scala to write new modules that plug into the application. I don't get the impression anyone is advocating writing lots of additional java code once scala enters the scene.
Peter Recore
This is a fair point. I have to say that I have kept most *legacy* systems as pure Java - but then none of ours are behemoths and most will probably be re-written from scratch on a 3-5 year cycle
oxbow_lakes
Behemoth is a great word for those types of systems, because it implies both huge and scary :)
Peter Recore