Enhancements In GScript

You may wonder how we get all those great closure-based methods like findAll() and sortBy() onto classes (or interfaces!) in GScript. The secret is a language construct called Enhancements. Here is a stripped down version that puts the findAll() method onto java.util.List:

  uses java.util.*

  enhancement MySampleEnhancement : List {

    function findAll( filter(T):Boolean ) : List {
      var returnList = new ArrayList()
      for( element in this ) {
        if( filter( element ) ) {
          returnList.add( element )
        }
      }
    }

  }

So what does all that say?

Well, we define an enhancement named MySampleEnhancement, which enhances the List interface and adds one method: findAll(). findAll() takes a closure which takes a single parameter that is the same type as the list and returns true or false. If the closure returns true, the element is included in the (strongly typed) return list.

We could now use this method like so:

  var lst = new ArrayList(){"a", "ab", "abc"}
  var results = lst.findAll( \ s -> s.length >= 2 )

And the results would be ["ab", "abc"] since those strings have a length greater than or equal to two.

Note the ArrayList got this enhancement method because it implements the List interface.

Enhancements let you put new functionality where it belongs: on the object the functionality affects, rather than in static methods on a Utility class or in a helper object. It also makes the functionality more discoverable since programmers will see it when they are coding in Studio. Finally, using enhancements lets you fully leverage the GScript languages features even in types that are defined elsewhere.

Pretty neat, eh?


2 Comments on “Enhancements In GScript”

  1. jb says:

    What version of ClaimCenter does this work with, we currently use 4.0.3.

  2. Carson Gross says:

    jb,

    Enhancements are a new feature in ClaimCenter 5.x, built on the Bedrock platform. When you guys upgrade to Bedrock you are going to have a huge number of new tools to work with. I hope it is fun.

    Cheers,
    Carson


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