开发者

How well do Java and Scala work together?

开发者 https://www.devze.com 2022-12-21 19:22 出处:网络
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 togethe

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 availabl开发者_开发知识库e on doing it? If I want to use Scala in web projects how to do it (other than lift)? All ideas and suggestions welcome.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号