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.

October 16, 2010

Groovy++ Goodies (1) – Closure Level Fields

Filed under: Uncategorized — Tags: , — roshandawrani @ 9:58 am

Groovy++ allows you to declare closure level fields using its new @Field annotation.

In core groovy, if you need to remember some state across multiple calls to the same closure instance, you need to do it using variable declared outside the closure scope. So, I see this Groovy++ feature as a useful goodie.

Below is some sample code to demonstrate the use of closure level fields:

@Typed void squares() {
    (1..20).each {
        @Field boolean lastOneSqaure = false
        @Field int which = 0
        
        if(lastOneSqaure) println "$it) Last number($which) was a square"
        
        double d = Math.sqrt(it)
        if((d - ((int) d) == 0)) {
            lastOneSqaure = true
            which = it
        } else {
            lastOneSqaure = false
        }
    }
}

squares()

The above code correctly outputs

2) Last number(1) was a square
5) Last number(4) was a square
10) Last number(9) was a square
17) Last number(16) was a square

The groovy++ compiler makes sure that the variables marked with @Field are declared as fields in the closure class and not as local variables – allowing you to preserve state across multiple invocations to a particular closure instance without needing any out-of-closure-scope assistance.

September 10, 2010

Added support to Groovy Compiler to allow source files with extensions of your choice

Filed under: Uncategorized — Tags: , — roshandawrani @ 6:36 pm

I am happy to finally push the change to groovy compiler so it allows users to mix and match the groovy source with multiple extensions of their choice – http://tinyurl.com/376f6mj.

For example, Groovy++ has *.gpp and *.grunit files. You may want to have *.g or *.gscript, or *.gdsl or *.gspec, or whatever else – and have a mix of any number of them.

The feature is currently available in 1.7.5 and 1.8.0-beta-2 snapshots, which are available from here –

All that needs to be done to use this feature is to have your extensions listed down in the file META-INF/services/org.codehaus.groovy.source.Extensions and make it available on the classpath and the groovy compiler will do the rest.

Here is a sample of how the file may look like:

# Format: one extension on each line without the leading “*.”
gpp
gscript
gdsl

Feedback welcome.

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: