Upcoming Gosu Events

There are some Gosu-related events coming up that people might want to consider attending:

July 12, 2011 6-9PM – Sacramento Java Users Group

Exit Certified

I’ll be giving a brief talk on the experimental Protocols typeloader for Gosu and, time permitting, I’ll talk about Ronin, a small Gosu-based web framework.

July 18-20th – JVM Language Summit

Oracle Santa Clara Campus

Most of the Gosu Team will be at the JVMLS: Scott McKinney (Gosu Team Lead), Dumitru Petrusca (IDE Lead), David Green (XML Guru) and me. I’ll be giving a talk on the Protocols typeloader, and implementing language features as a library using The Open Type System.

July 25-29th – OSCON 2011

Oregon Convention Center, Portland, OR

I’ll be at OSCON this year, giving an introduction to Gosu And The Open Type System.

If you are going to be at any of these and want to meet up and talk Gosu, or just drink beer, let me know: email or twitter.

In Defense Of “Name : Type”

A big (really *the* big) syntactic departure that Gosu took from Java is around type declarations. In Java, you have the time-honored c-style of declaration: type, followed by name:

  public String aMethod(String aParameter) {
    String theString = "The String " + aParameter;
    return theString;

In Gosu, you have the name first, followed by a colon, then the type:

  function aMethod(aParameter : String ) : String {
    var theString : String = "The String " + aParameter
    return theString

(Note that you would typically omit the local variable’s type in Gosu and let type inference do its thing.)

This syntax comes down to us from Pascal and is the standard in ECMAScript, which is the language Scott originally based Gosu on.

Now, this is just syntax. Not really a big deal, right?

Turns out, in practice, this actually is kind of a big deal to people. Let me quote Stephen Colebourne:

@Carson, thanks for writing about Gosu. The feature literal element looks good, and fits my view of how such things should work with # for the meta level. Unfortunately, Gosu declares variables as name : type, which makes it rather unappealing to me. Fix that, and I might take a real look at the language…

Despite the fact that Gosu has everything that Stephen wants with respect to properties, he won’t take a real look at the language because of the syntax around types. So this obviously is a big deal to Stephen, and I can’t say I blame him: as Oscar Wilde said, only a very shallow person does not judge by appearances.

So, while admitting that it will look a little strange the first time to Java developers, let me try to give a small defense of the Gosu type syntax.

First off, I think that the Gosu syntax reads more naturally once you get used to it. Taking the example method above, reading it left to right in Java gives:

“Returning a String, the method ‘aMethod’ takes a String called ‘aParameter'”

In Gosu you have:

“The function ‘aMethod’ takes ‘aParameter’ of type String and returns a String”

I believe that most english speakers would find the latter a more natural way to explain the method. This is subjective of course, but, hey, we are talking about syntax here.

A less subjective advantage of Gosu’s syntax becomes apparent when you look at generic methods.

Consider this example:

   <T> T aGenericMethod(T aParameter) {
    return aParameter;

Note that in Java the generic type variable must be declared first, off in space, so that it can precede the return type of the method, which could (but need not) contain a reference to the type variable.

In Gosu, this is expressed much more naturally:

   function aGenericMethod<T>(aParameter : T) : T {
    return aParameter

Because the return type of the method comes at the end, the type variable can nestle comfortably after the name of the function. As a bonus, this is much closer to the syntax of generic classes, with the type variable following the name, rather than preceding it.

This has ramifications at the call site, as well. In Java, you must prefix the function call with a type if you want to give an explicit parameterization:

  anObject.<String>aGenericMethod( "Yay generics!" );

In Gosu, again, you use a more natural syntax:

  anObject.aGenericMethod<String>( "Yay generics!" )

Note how similar this is to the syntax used for class parameterization (e.g. ArrayList<String>).

As an interesting corner case, in Java you simply can’t invoke a parameterized generic method without a preceding expression:

  <String>aGenericMethod( "Yikes" );

will not compile, whereas this will:

  this.<String>aGenericMethod( "Hmmm" );

Gosu, on the other hand, has no such restriction, since the method parameterization comes comfortably after the method name:

  aGenericMethod<String>( "It just works" );

Now, these are admittedly fairly small advantages. But, nonetheless, they are advantages. I hope that outlining them will convince some of you who are skeptical about the “name : Type” syntax to look more deeply at Gosu: it is a very nice language with some interesting features. (Not the least, the Open Type System, which Scott wrote about here.)

Feature Literals + Enhancements + Blocks == Properties++

Stephen Colebourne recently commented on the lack of formal properties in Java:


I completely agree with Stephen that properties should have been introduced to Java long ago: they were one of the first things that Scott added to Gosu and they have been incredibly useful at Guidewire. They aren’t really sexy or novel, but they solve a lot of practical problems.

In the latest release of Gosu, we incorporated feature literals, which allow you to reference a property (or method) using the # operator, making properties even easier to work with. A commenter on Stephen’s blog mentioned Gosu’s feature literals and had some nice things to say about our implementation, but mentioned that they do not support a listener infrastructure, where you can register a listener with a property to be invoked when the property is written or read.

Gosu obviously can’t provide a general listener infrastructure: the Open Type System works with all sorts of different types, each with different property implementations, storage models and so on. *However*, using feature literals, blocks, generics and enhancements, you can add some pretty cool support for property listeners on types that can support them.

I’ve thrown together a quick example that shows how this can be accomplished.

The entirety of the code in this example is up on github here:


First, let’s create a simple class that encapsules property storage and supports callbacks:

package props
uses java.util.HashMap
uses java.util.ArrayList

class PropHolder {
  var _props = new HashMap<String, Object>()
  var _callback = new HashMap<String, List<block(val:Object):Object>>()
                    .toAutoMap( \ s -> new ArrayList<block(val:Object):Object>() )
  function setProp( name : String, val : Object ) {
    for( cb in _callback[name] ) {
      val = cb( val )
    _props[name] = val
  function getProp( name : String ) : Object {
    return _props[name]
  function addPropListener( name : String, blk : block(val:Object):Object ) {
    _callback[name].add( blk )

This is some simple infrastructure that just wraps a hash map to store properties in, and maintains a list of listeners to execute when a property is invoked.

Next, let’s create a simple gosu class that uses this infrastructure to store a property:

package props

class ExampleGosuClass implements IHaveListenableProperties {
  var _props = new PropHolder() 
  property get Prop1() : String {
    return _props.getProp( "Prop1" ) as String

  property set Prop1( s : String ) {
    _props.setProp( "Prop1", s )
  override function addListener( propName : String, listener : block(Object):Object ) {
    _props.addPropListener( propName, listener )
  property get ThisAsProp() : ExampleGosuClass {
    return this

This class implements a simple interface, IHaveListenableProperties:

package props

interface IHaveListenableProperties {
  function addListener( propName : String, listener : block(Object):Object );

Finally, and this is where we get tricky, so follow along closely, we are going to enhance a specific parameterization of the return type of the expression foo#Bar, which is the API class gw.lang.reflect.features.BoundPropertyReference:

package props

uses gw.lang.reflect.features.BoundPropertyReference

enhancement BoundPropertyRefEnhancement : BoundPropertyReference<IHaveListenableProperties, Object>  
  function addListener( blk : block(obj:Object):Object ) {
    var root = this.Ctx as IHaveListenableProperties
    root.addListener( this.PropertyInfo.Name, blk )

Note that this method will only appear on BoundPropertyReference‘s that have a IHaveListenableProperties object at their root, due to the particular parameterization that we have chosen to enhance. (Cool trick, eh?)

So, with all that infrastructure in place, we can now write this code:

uses props.*

var ex1 = new ExampleGosuClass()
var ex2 = new ExampleGosuClass()

ex1#Prop1.addListener( \ o -> { print( o ); return o + " Yay!" }  )
ex1.Prop1 = "Hello Listeners!" // prints "Hello Listeners!" 
print( ex1.Prop1 )             // prints "Hello Listeners! Yay!"

ex2.Prop1 = "Nope, no listener..."  // no listener, so no printing
print( ex2.Prop1 ) 

In this code, we create two instances of ExampleGosuClass and add a block-based listener to the Prop1 property on ex1. We then set the property on ex1, which invokes the block and prints out the original value, then transforms the value before storing it. Finally, we do the same with ex2 to demonstrate that the listener is bound only to the Prop1 property on ex1.

So we’ve used feature literals, enhancements and blocks to implement a slick little properties listening system in Gosu. A system like this could be integrated into, for example, an O/R framework or a web framework even more elegantly since the listener infrastructure could be entirely internal.

Just another example of the excellent tools in Gosu available for framework builders. I think you’ll see a lot of innovation in this space in the next year or so, as our tools mature and Gosu becomes more widely known.