First write a Scala class:
Complex.scala
Compile this using scalac:
Now write our Groovy Program:
ComplexMain.groovy
Now run the program (assuming scala-library.jar is in the CLASSPATH):
Which produces:
1.2+3.4i
Note that in this example it would have been just as easy to write our Complex class using Groovy as follows:
but in other cases you may have some existing Scala classes you wish to reuse from Groovy.
Labels
1 Comment
Hide/Show CommentsMay 12, 2009
Daniel Spiewak
The Scala example given here is not at all idiomatic Scala. For one thing, `def` defines a method, not a field. For another, it would have been just as easy to write in only three lines instead of five:
class Complex(val real: Double, val imaginary: Double) {
override def toString = "" + real + (if (imaginary < 0) "" else "+") + imaginary + "i"
}