Friday, February 09, 2007

Grails 0.4.1 out, Groovy just got more dynamic

Haven't posted in a while, but the focus for the last few weeks, Grails wise that is, has been getting 0.4 out and then putting out a sensible point release from community feedback. Grails 0.4.1 is that point release and is out now.

So now that things have settled down a bit I want to talk a little about one of the features in Grails 0.4 that really excites me. It is called the ExpandoMetaClass and what it does is bring to Groovy easy addition of dynamic features kind of like in Ruby's meta stuff and Javascript's prototype object.

Groovy has always had the underlying infrastructure to make magic happen, it has just been hard to get to for Joe user, particularly when coming from other languages with these features. In Grails, this is no longer the case and the plan is to move this functionality into Groovy core in the future. So how does it work in Grails?

Well say you have a class, it could be a Java or a Groovy class, and you want to add a method to it. Let's take java.lang.String for example. Say you wanted to add a new method that takes an existing String and swaps the casing of the String so upper case letters are in lower case and vica versa. This is how you do it:

String.metaClass.swapCase = {->
def sb = new StringBuffer()
delegate.each {
sb << (Character.isUpperCase(it as char) ? Character.toLowerCase(it as char) :
Character.toUpperCase(it as char))

assert "UpAndDown" == "uPaNDdOWN".swapCase()

Job done. Notice the special use of the implicit "delegate" variable that equates to "this" inside the method. Now the nice thing is it is not just methods you can do this for. You can add constructors, properties, instance methods, and static methods (checkout the docs for more info) on any Java OR Groovy class. Hopefully this will help make Groovy Meta programming more accessible.


Jase said...

I'm worried that metaprogramming in Grails/Groovy may obfuscate code as well as C++'s operator overloading did. Are you sure this is a good idea to do?

Anonymous said...

Looks great. What's the difference between this and Groovy Categories?

Graeme Rocher said...

There are a number of differences between this and categories. First this will change every class within the VM whilst categories only apply for the block which you use the use statement with:

use(MyCategory) {


Next there are limitations as to what you can do with a category in terms of adding contructors, static methods etc.

There are no such restrictions here.

Sam said...

As the original contributor of Categories I'm surprised that this is going in. The reason we didn't add it the first time was because in a multithreaded world that Ruby rarely ventures into this kind of thing is very unpredictable whereas Categories had well defined behavior.

Graeme Rocher said...

Hi Sam,

Indeed you are correct, and it will be disabled by default within Groovy.

However there are many circumstances where you ARE in control of threading and the environment. In this case it makes sense to offer a simpler way to add dynamic features.

Categories, although helpful, are counter-intuitive for many. With regards to Ruby and Javascript, these languages also have problems when it comes to dealing with threads (green threads in Ruby, real threads in JRuby) and concurrent modifications of meta classes (prototype object in JS)

However when used sensibly (ie you do all of the method registration at startup time) there is little issue. It seems there is a unfounded fear of this in Groovy land. Sure you can shoot yourself in the foot, but as I keep saying "With great power comes great responsibility" ;-)

Anonymous said...

It is the 2moons dil which make me very happy these days, my brother says 2moons gold is his favorite games gold he likes, he usually buy some 2moon dil to start his game and most of the time he will win the buy 2moons dil back and give me some cheap 2moons gold to play the game.

Anonymous said...




Anonymous said...

Many people want to in the game had more archlord gold, but in the end always do not come true, so only relp on buy archlord gold, so for the students this is a big different, so we all need the cheap archlord gold, so we all hope.

Anonymous said...

^^ nice blog!! ^@^

徵信, 徵信網, 徵信社, 徵信社, 感情挽回, 婚姻挽回, 挽回婚姻, 挽回感情, 徵信, 徵信社, 徵信, 徵信, 捉姦, 徵信公司, 通姦, 通姦罪, 抓姦, 抓猴, 捉猴, 捉姦, 監聽, 調查跟蹤, 反跟蹤, 外遇問題, 徵信, 捉姦, 女人徵信, 女子徵信, 外遇問題, 女子徵信, 外遇, 徵信公司, 徵信網, 外遇蒐證, 抓姦, 抓猴, 捉猴, 調查跟蹤, 反跟蹤, 感情挽回, 挽回感情, 婚姻挽回, 挽回婚姻, 外遇沖開, 抓姦, 女子徵信, 外遇蒐證, 外遇, 通姦, 通姦罪, 贍養費, 徵信, 徵信社, 抓姦, 徵信, 徵信公司, 徵信社, 徵信公司, 徵信社, 徵信公司, 女人徵信,

徵信, 徵信網, 徵信社, 徵信網, 外遇, 徵信, 徵信社, 抓姦, 徵信, 女人徵信, 徵信社, 女人徵信社, 外遇, 抓姦, 徵信公司, 徵信社, 徵信社, 徵信社, 徵信社, 徵信社, 女人徵信社, 徵信社, 徵信, 徵信社, 徵信, 女子徵信社, 女子徵信社, 女子徵信社, 女子徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信社,

徵信, 徵信社,徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 外遇, 抓姦, 離婚, 外遇,離婚,

徵信社,徵信, 徵信社, 徵信, 徵信社, 徵信,徵信社, 徵信社, 徵信, 外遇, 抓姦, 徵信, 徵信社, 徵信, 徵信社, 徵信, 徵信社, 徵信社, 徵信社, 徵信社,徵信,徵信, 徵信, 外遇, 抓姦