8

Is this allowed in Scala code:

DomNode node = node.getFirstChild()

where DomNode is Java type from external java library and getFirstChild() is defined on DomNode type.

I am porting existing java program to scala and it would be very convenient if I leave original java declerations as is to minimize porting efforts.

4
  • 2
    They compile to the same bytecode format anyway and interop of the compiler output is possible, so why port Java code to Scala if you aren't going to change it to use Scala features? Commented Jun 15, 2011 at 12:52
  • @delnan Perhaps to gradually port it to idiomatic Scala over time? Commented Jun 16, 2011 at 19:10
  • @Aaron: Since interop is perfectly possible and encouraged, wouldn't it be more sensible to rewrite one module in idiomatic Scala in one shot (or factor a part that's part to do that out into its own module) and make it work with the remaining Java code? Commented Jun 16, 2011 at 19:16
  • @delnan It depends a lot on the size of the code base. Although interop is possible, it can be quite a hassle, so if the codebase is small enough I might prefer to port to non-idiomatic Scala all at once and then gradually port to idiomatic Scala. Commented Jun 16, 2011 at 19:19

2 Answers 2

21

You can use Java classes in a Scala program, but you would ofcourse have to use Scala syntax:

val node: DomNode = node.getFirstChild()

You cannot use Java syntax in the form Type variableName.

edit (thanks to ericacm) - You can also just specify

val node = node.getFirstChild()

so you don't have to specify the type of node explicitly; you can let Scala infer the type.

Sign up to request clarification or add additional context in comments.

4 Comments

(I'm rather new at Scala, but don't you need a var or val there?)
@Jonathan that depends, a val is immutable and should be preferred, but if you need a mutable variable, use var.
val means non-reassignable ("final"), not immutable.
You can also use val node = node.getFirstChild. The cleaner code provided by type inference.
13

IntelliJ IDEA can translate from Java to Scala for you. If you paste Java code into a ".scala" file IntelliJ IDEA notices it and asks you if you would like to try an automatic conversion. You might wanna check it out.

PS

I never tried it out myself...

4 Comments

It is a good starting point. Won't be idiomatic scala, but will generally work.
I used this to port portions of the GWT Showcase sample app to Scala (github.com/anovstrup/scalagwt-sample). The conversion wasn't perfect, but it certainly saved a lot of effort.
I support this: IDEA's Java to Scala conversion is not perfect but definitely helpful. My main issue with it at this time is that it doesn't copy over comments.
@ebruchez Similarly, it reorders class members and drops blank lines in code blocks (i.e. newlines that don't affect the AST yet enhance readability). Note: this comment may be out-dated; it's been a while.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.