Skip to end of metadata
Go to start of metadata

First write a Scala class:


Compile this using scalac:

Now write our Groovy Program:


Now run the program (assuming scala-library.jar is in the CLASSPATH):

Which produces:


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.

  • No labels

1 Comment

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