Using enhancements

You use Gosu enhancements to augment classes and other types with additional concrete methods and properties. You can also use enhancements to overcome the language shortcomings of Java or other languages defining a class or interface. For example, Java-based classes and interfaces can be used from Gosu, but they do not natively allow use of blocks, which are anonymous functions defined in-line within another function. Gosu includes many built-in enhancements to commonly used Java classes, so any Gosu code can use them.

For example, Gosu extends the Java class java.util.ArrayList so that you can use concise Gosu syntax to sort, find, and map members of a list. These list enhancements provide additional methods to Java lists that take Gosu blocks as parameters. The original Java class does not support blocks because the Java language does not support blocks. However, these enhancements add utilities without direct modifications to the class. Gosu makes these additional methods universally available wherever Gosu code uses java.util.ArrayList.

You can also enhance an interface. An enhancement does not add new methods to the interface itself, nor add new requirements for classes that implement the interface. Instead, enhancing an interface provides all classes that implement the interface with new methods and properties. For example, if you enhance the java.util.Collection interface with a new method, all collection types have your newly added method.

Although you can add custom properties to existing entities, those new properties do not appear in the generated Data Dictionary documentation that describes the application data model.

Syntax for using enhancements

Using an enhancement requires no special syntax. The new methods and properties are automatically available within the Gosu editor for all Gosu contexts.

For example, suppose there is an enhancement on the String type for an additional method called calculateHash. Use typical method syntax to call the method with any String object accessible from Gosu:

var s1 = "a string"
var r1 = s1.calculateHash()

You could even use the method on a value you provide at compile time:

"a string".calculateHash()

Similarly, if the enhancement adds a property called MyProperty to the String type, you could use code such as:

var p = "a string".MyProperty

The new methods and properties all appear in the list of methods that appears if you type a period (.) character in the Gosu editor. For example, suppose you are typing s1.calculateHash(). In typing this statement, after you type s1., the list that appears displays the calculateHash method as a choice.