bits and pieces

June 2, 2011

Groovy++: Compile your code in modes @Typed / @Mixed / @Dynamic now

Filed under: Uncategorized — Tags: — roshandawrani @ 5:38 pm

Groovy++ supports 3 compilation modes, allowing various degrees of strictness with which its compilers processes the source code:

  • @Typed
  • @Typed (TypePolicy.MIXED)
  • @Typed (TypePolicy.DYNAMIC)

In dynamic mode, Groovy++ tries to be as lenient as core Groovy, and in mixed mode, it tries to give the best of both the words – type safety checks, improved performance, etc, where it can and allows nice, dynamic features of core Groovy at other places.

With Chanwit Kaewkasi‘s help, the usage of (more verbosely written) compilation modes @Typed (TypePolicy.MIXED) and @Typed (TypePolicy.DYNAMIC) can now be replaced by @Mixed and @Dynamic respectively.

Here is a short snippet showing their usage:

@Typed class Test { /* whole class is statically compiled unless overridden */
    String STR = "some str"

    static main(args) {
        def t = new Test()
        t.getLengthInMixedMode(STR)
        t.getLengthInDynamicMode(STR)
    }

    @Mixed getLengthInMixedMode(str) {
        def extra = "extra"
        def extraLen = extra.length() // type inferred here
        str.length() /* allowed due to @Mixed mode */ + extraLen 
    }

    @Dynamic getLengthInDynamicMode(str) {
        str.length() /* allowed due to @Dynamic mode */
    }
}

So, the new list of Groovy++ compilation modes is:

  • @Typed
  • @Mixed
  • @Dynamic

Note:
* Fully static mode continues to be named as @Typed.
* If used, @Typed(TypePolicy.MIXED) and @Typed(TypePolicy.DYNAMIC) continue to work as before.

The Silver is the New Black Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: