ace
ace

Reputation: 12024

Mixing Java code in Scala program?

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.

Upvotes: 8

Views: 15645

Answers (2)

Jesper
Jesper

Reputation: 206776

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.

Upvotes: 21

agilesteel
agilesteel

Reputation: 16859

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...

Upvotes: 13

Related Questions