Omgo's Blog

October 21, 2009

Expandometaclass gotcha

Filed under: Groovy — aswin @ 9:10 pm

I hit a gotcha while trying out some basic Meta stuff in groovy. Say you wanted to replace the default “toString()” method of your String class with the following groovy code

String.metaClass.toString = {"completely useless"}
println new String("world saver").toString()

I was expecting to see the “completely useless” as answer, but turns out that the toString() method did not get overridden. After spending some time analyzing what was going on looks like the method being used for the invocation is actually the one from the super interface of String , CharSequence. This handle is used by the metaclass system and it resolves to the actual toString method implemented in the String class itself , completely ignoring the metaclass version we added. So what needs to be done to get the effect is to rewrite the code to look like

CharSequence.metaClass.toString = {"completely useless"}
println new String("world saver").toString()

and you get “completely useless”.  This is not exactly what one would want,  as this would affect all the subclasses that has implemented this method and all of them would return “completly useless” when their toString() method is invoked.   In short if you have a superinterface method you are trying to override (or replace I must say) using ExpandoMetaclass facility , the chances are that it would have this issue, so plan ahead 😉

ExpandoMetaClass and Metaclass system itself is getting rewritten for the Groovy 2 release I guess, so hopefully these kind of issues would go away.

Advertisements

December 24, 2008

The dynamic language choice of Enterprise

Filed under: Groovy — aswin @ 4:58 pm

With language wars looming again, this time more on the dynamic nature, expressiveness and flexibility I think its important for the enterprises to standardize on one of these.  There are definite merits of identifying and standardizing on this as there are many areas where the productivity gains brought in by these languages are unparalleled.

The main contenders in this space for me are Groovy, Ruby and Python.  All the three languages have comparable functionality and good support for the web with powerful web frameworks that makes creating web application a breeze.  Ruby has Rails, Groovy has Grails and Python has Django as their web frameworks.

Groovy shines from it roots in Java and JVM and offers an easy migration for all those enterprises with  roots in  Java and Jvm.  The groovy community is steadily growing and with the recent acquisition by SpringSource,  looks like its going to be  very important in the java world .  Also the approach taken by the community is to utilize and integrate existing proven solutions and offer them with all the power and expressiveness of the dynamic language. Grails is the best example of this as it use Hibernate, Spring , Sitemesh etc and plug in support for many others such as Acegi, Jsecurity, JBPM etc. All the benefits of years or research and adoption with ease of use of a dynamic language and that too on a proven platform , the mighty JVM.  In fact the importance of the dynamic languages on the JVM has grown so much that the JVM folks are considering specific support , such as adding special instruction sets to it. The only place I think where Groovy lacks is the adoption in the .Net world (currently is supported through IKVM ), but there are discussion on making that easier (http://groovy.codehaus.org/Groovy.Net+module+discussion).

Ruby’s main advantage is going to be the active projects it has on both JVM (Jruby) and .Net (IronRuby) and also as the standalone language depending on the just the c libraries.  Ruby on Rails was the first framework that showed the world the power of web application framework built on a dynamic language. There is pretty good adoption of the framework  and lot of community support, so I think ruby is going to be around as well.  As the platform speicfic version can take advantage of all the good stuff that are already provided, there is a lot you could do with Ruby. I am not a huge fan of the syntax, as it differs a lot from the familiar C , C++, Java world and anyone coming from that background might find it a bit different.

I think the main selling point of Python is the support from Google :). In fact I was surprised when Google announced their cloud computing model with exclusive support  for the Django framework!. We also know that python is used extensievly inside google, so eventually we might see the fruits of that labour made available to the public.

Create a free website or blog at WordPress.com.