the Holy Grails!

What’s it?

Well, Grails is a web-framework based on a JVM (Java Virtual Machine, or just VM that’s how Jonathan Schwartz, Sun’s CEO is calling it) scripting language called Groovy.

Groovy?

I think is better to start explaning what’s Groovy.

Groovy is an scripting language that adds powerful additions to Java language like virtual methods, meta-programming, closures and lot more!

Why Groovy?

Well, Groovy is an excelent tool for Java programmers. Why have i said JAVA PROGRAMMERS and not just PROGRAMMERS?
I know that you have already heard of Ruby (and Ruby on Rails), Python (and Turbo Gears/Django) or even Scala (and maybe Lift). They’re incredible powerful scripting languages, and their frameworks are as good as them.

But they’re not Java. Their syntax not even looks like Java.

Ok, they all have counterparts on (J)VM, but… it’s not the same as programming in our favorite language.

Well, now imagine all the power of those language in a Java syntax. That’s Groovy.

A class in Ruby looks like this:

class Person
attr_reader :name, :address

def initialize name,address
@name    = name
@address = address
end

def rent_a_new_house address
@address = address if address
end

def to_s
“Name:#{@name} — Address: #{@address}”
end

end

For an guy comming from Java, that’s hebraic.
Now, if i say this’s a Groovy class:

class Person implements Comparable{
String name
String address

String toString(){
“Name: ${this.name} — Address: ${this.address}”
}

int compareTo(other){
name?.compareTo(other?.name)
}
}

The same guy cannot say he cannot understand this.
It’s Java.
But it’s not.

It’s Groovy!

Groovy adds a thing to Java called code by convention, a thing that came a long time a go in a far language… (that, i have to be honest, i don’t remember the name)

If you look to that Person class, you will say:

Where are the privates?
Where are the getters and setters?
Where is the constructor?
And WTF is that ToString?

Imagine something like that:

Everytime you create a class on your IDE of choice, you have to declare each attribute private, and them, see which one of them needs a getter or a setter. Then, you create a toString, and equals.

That’s boring, and still, you have to do it.

With Groovy’s coding by covention, to declare an private attribute along with its getter and setter all you have to say is: String name. or int age.

That’s it.

I know that there’re other things on that class that are not Java.
I’ll explain those.

First, toString:

String toString(){
“Name: ${this.name} — Address: ${this.address}”
}

The first thing you will notice on that method is there’s no return statement.
(The other thing is the ${} thing.)

On Groovy, as on another languages, you don’t have to explicitly say return name — all you have to do is add the thing you and to return as the last line of your method, i.e.:

def getName(){
this.name //return name!
}

The other thing, the ${} thing is called GString expression and it’s just like an JSP EL or and JSF EL. The variable name inside the curly brackets will be resolved and the string will be a lot prettier than something like:

“Name: ” + name + ” Address : ” + address

And with the benefit of calling methods insed the #{}!

There’s a lot more with Groovy. Too much, i think so from now, that’s it.
For more info: http://groovy.codehaus.org/

Stay tuned!

Advertisements
This entry was posted in technology and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s