LEARN GROOVY

Syntax

Groovy’s syntax can be made far more compact than Java. For example, a declaration in Standard Java 5+ such as:

 for (String it : new String[] {"Rod", "Carlos", "Chris"})
     if (it.length() <= 4)
         System.out.println(it);

can be expressed in Groovy as:

 ["Rod", "Carlos", "Chris"].findAll{it.size() <= 4}.each{println it}

What you see in the parentheses \{ \} is a magical *Closure * . What make Groovy so powerful compare to Java! We will learn about it later.

Official syntax description

For your overview, I will introduce the most important syntax of the language:

Logical Branching

Looping

Operation

Highlight language features

Beside with elegant syntax, Groovy has features you always wish that Java have, below paragraph will call out few features that actually help a lot in Game programming:

Operation overload

Collections

Reflection

Closure

Read more about it here:

http://groovy-lang.org/Closures link:http://groovy-lang.org/Closures+-Informal+Guide[http://groovy-lang.org/Closures-Informal+Guide] link:http://groovy-lang.org/Closures-Formal+Definition[http://groovy-lang.org/Closures-+Formal+Definition]

What is a Closure?

A Groovy Closure is like a “code block or a method pointer. It is a piece of code that is defined and then executed at a later point. It has some special properties like implicit variables, support for currying and support for free variables (which we’ll see later on). We’ll ignore the nitty gritty details for now (see the formal definition if you want those) and look at some simple examples. Simple Example

def clos = { println "hello!" }

println "Executing the Closure:"
clos()                          //prints "hello!"

Note that in the above example, “hello! is printed when the Closure is called, not when it is defined.

Meta-programming

Here come the Good! For those who remember the power of JavaScript’s prototype keyword, you can do them same with Groovy. For those who don’t even know what JavaScript is, Meta-programming is the way to expand the class with methods and attributes on the fly.

Groovy Builder – SwingBuilder

Groovy use a lot power the of Builder pattern.

In the snippets below I will show you write a few builders to speed up your JME code. The full list of Builder come in Part3 and 4.

Language comperation

JavaScript comperation