Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Excerpt

Provides integration with the Scala Programming language

Description

@Scalify simplifies the task of integrating Groovy and Scala code. This transformation adds the required bytecode to your Groovy classes to make them appear as native Scala classes. It also helps when implementing or extending a Scala trait/class from Groovy.

Usage

Place @Scalify at the class level, example;

Code Block
// -- begin scala code
import scala.reflect.BeanProperty  

trait Output {  
   @BeanProperty var output: String  
} 

// -- end Scala code

// -- begin Groovy code
import groovyx.transform.Scalify  
  
@Scalify  
class GroovyOutput implements Output {  
   String output  
}  

When the trait Output is compiled it will generate bytecode similar to

Code Block
public interface Output {
  public abstract void setOutput(java.lang.String);
  public abstract java.lang.String getOutput();
  public abstract void output_$eq(java.lang.String);
  public abstract java.lang.String output();
}

The first pair of methods is your typical POJO accessors (generated by Scala's @BeanProperty) which Groovy can handle quite well. The second pair of methods represent Scala's native accessors, these will be written by @Scalify (if not present already in your class definition). Additionally @Scalify will make sure that your class implements scala.ScalaObject if it does not already.

One last trick up the sleeve is that @Scalify will generate operator friendly methods, these are the currently supported methods:

Operator

Groovy

Scala

+

plus()

$plus()

-

minus()

$minus()

*

multiply()

$times()

/

div()

$div()

%

mod()

$percent()

^

xor()

$up()

&

and()

$amp()

|

or()

$bar()

**

power()

$times$times()

<<

leftShift()

$less$less()

>>

rightShift()

$greater$greater()

-

negative()

unary_$minus()

+

positive()

unary_$plus()

~

bitwiseNegate()

unary_$tilde()

Warning
titleFull compatibility

While @Scalify can generate all the require boilerplate code it cannot (at the moment) generate a required class attribute (called pickle) that the Scala compiler uses to gather metadata information about a Scala class. This means you won't be able to call a property setter using the short notation, in other words

Code Block
new GroovyOutput().output = "something"

will result in a compilation error while the following will work

Code Block
new GroovyOutput().output_$eq("something")

Dependencies

Make sure to have the Scala compiler & libraries on your classpath. You will also need asm, grab the latest version available with your Groovy distribution