Sunday, April 22, 2007

GScript - You don't Have to Use Ruby to be Cool

So why do you think Ruby is cool?

If you are like me who work mainly on Java platform, then you probably like Ruby because how much less code you write in Ruby to achieve the same result. Now, if you think about it, not all of them are because of the type-free concept.

With the way that generics goes (what it was and what it is), we can see that the design of Java language comes with some tough trade-offs. After all, I don't suppose you want to piss off the big vendors who keep the money rolling.

Well, what if we can start fresh?

Check this one out...

var list = new ArrayList({one, two})
// this creats a map of beans with ID as key.
var idMap = list.partition(\bean -> bean.ID)
A few notes:
  • The last statement uses closure, in GScript syntax. For anyone familiar with Ruby, this call would be something like "list.partition {|bean| bean.id}"
  • The expression "bean.ID" is actually a "bean.getID()" method call, which is declared as property getter in GScript
  • Even though "list" is a type of List, you can add "Enhancement" to this interface so that you can have additional behavior built on top of existing public methods. This gives you some of the benefit of "mixing" without sacrificing the type safety. If you use apache commons library extensively, you probably have a list of enhancement that you can write already.
  • The type is inferred so you really need to type once. (Note that the type of variable idMap will be inferred as Map<Key, KeyableBean>, a generic map instead of just a Map)
  • Create the list like the way you mean it instead of the Arrays.asList syntax.
You have just caught a glimpse (and I do mean "glimpse") of GScript, the language that Guidewire developed on top of Java. You want a rule engine that can be configured? What about a rule engine that can be programed and deployed without bouncing the server?

There are talks about making this public, by which time you will be able to see the full power of it. Before that, you can consider coming in for an interview. :)

9 comments:

Anonymous said...

The code in that article does not use a closure.

Shane said...

I misspoke when I said "I am sure...".

I have updated the post to make my point clear. Thanks for the comment.

Anonymous said...

You misunderstand. The code in the article uses an anonymous function. The anonymous function does not have any free variables and therefore does not close over the variables in its environment. It is therefore *not* a closure.

Shane said...

You are right. This is a simple example to prove the point. With GScript you cane use real blocks and define the variables and call into other functions.

Anonymous said...

What does a GScript anonymous function look like that writes to a variable in the outer environment?

Shane said...

I think variables are just shared, period.

When the semantics were designed, there was a "pop quiz" passed around to figure out what is the one that feels "natural", so I am pretty sure it is implemented.

With that said, I have to admit that I have not had chance to use it long enough to have a case to use it that way.

Anonymous said...

Thats all very nice, but being an (almost : ) happy user of GScript (yeah, I'm your customer employee) I think GScript really lacks of:
- decent IDE for developing (as Eclipse plugin) instead of homegrown Studio;
- way of unit- and mock-testing GScript code;

just my 2 cents..

Shane said...

Thanks for dropping by! You voice has already been heard. As a general rule, I should not promise anything publicly. You probably already noticed that most of the stuff in this post is not available in your version of Guidewire software. I'd suggest you go through the correct channel to keep yourself updated.

The only thing that I have to disagree with, respectfully, is the comment about using Eclipse. I wrote two plugin (one of which is jBehave runner) on Eclipse and it was not fun. Having full control of the design has given us great power to do a lot of cool stuff.

Who knows, maybe one day you will use a feature that I write and we can go grab a drink and chat.

Shane said...

I have deleted some discussion here upon the author's request