What is Gosu?
Gosu is the open-source programming language used in 700+ P&C core implementations, hundreds of third-party P&C apps, and in the work of tens of thousands of P&C consultants and developers. A powerful and easy-to-use object-oriented language, Gosu combines the best features of Java, including:
- Compatibility with existing Java libraries
- Significant improvements like blocks and powerful type inference
- These features change the way you write code.
Now you can write readable, powerful, and type-safe type code built on top of the Java platform. Many of the configuration and integration points in the base configuration of Guidewire use Gosu classes or expressions. To integrate with external systems, you can use native web service and XML support built directly into the language. You can work with XSD types or external APIs like native objects. For these reasons and more startups and some of the largest and most complex companies all around the world use Gosu every day in their production servers for their most business-critical systems.
Gosu is a general-purpose programming language built on top of the Java Virtual Machine (JVM). Because Gosu uses the JVM, Gosu has the following characteristics:
- Java compatible, so you can use Java types, extend Java types, and implement Java interfaces
- Object-orientation
- Easy to learn, especially for programmers familiar with Java
- Imperative paradigm
As well as the many features that Gosu shares with Java, Gosu includes the following additional features:
- Static typing, which helps you find errors at compile time
- Type inference, which simplifies your code and preserves static typing
- Blocks, which are in-line functions that you can pass around as objects. Some languages call these closures or lambda expressions
- Enhancements, which add functions and properties to other types, even Java types. Gosu includes built-in enhancements to common Java classes, some of which add features that are unavailable in Java (such as blocks)
- Generics, which abstracts the behavior of a type to work with multiple types of objects. The Gosu generics implementation is 100% compatible with Java, and adds further powerful improvements
- JSON support and dynamic typing
- Structural types, which relate classes to one another based on shared characteristics rather than interfaces or inheritance
- Dimension types, which include a unit as well as a valu.
- Composition, which delegates the implementation of an interface to another class
- Transactions for database integrity
- XML/XSD support
- Web service (SOAP) support
- String templates
- Concurrency
- Checksums
Gosu Key Features
Control Flow
Gosu has all the common control flow structures, including improvements on the Java versions. Below are some common uses and examples.
Gosu has the familiarif
,else if
, andelse
statements:if (myRecord.Open and myRecord.MyChildList.length > 10) { // Some logic } else if (not myRecord.Open) { // Some more logic } else { // Yet more logic }
Gosu permits the more readable English words for the Boolean operators:and
,or
, andnot
.
Alternatively, you can use the symbolic versions from Java (&&, ||, and !
).
Thefor
loop in Gosu is similar to the Java 1.5 syntax:for (ad in addressList) { print(ad.ID) } //If you need an index within the loop, use the following syntax to access the zero-based index: for (ad in addressList index i) { print(ad.ID + " has index " + i) }
Gosu has native support for intervals.
// a closed interval example
for (i in 1..10) {
print(i)
}
// an open interval example
for (i in new ColorInterval("red", "blue")) {
print(i)
}
The Gosu switch statement can test any type of object, with a special default case at the end:
var x = "b"
switch (x) {
case "a":
print("a")
break
case "b":
print("b")
break
default:
print("c")
}
Blocks
Gosu supports in-line functions that you can pass around as objects, which are called blocks. Other languages call blocks closures or lambda expressions. Blocks are useful as method parameters, in which the method’s implementation generalizes a task or algorithm but callers provide code to implement the details of the task. For example, Gosu adds many useful methods to Java collections classes that take a block as a parameter. That block could return an expression, such as a condition against which to test each item, or could represent an action to perform on each item. Below are some common uses and examples.
The following Gosu code makes a list of strings, sorts the list by string length, and then iterates across the result list to print each item in order:
var strings = { "aa", "ddddd", "c" }
strings.sortBy(\ str -> str.length).each(\ str -> { print(str) })
If an anonymous inner class implements an interface that has exactly one method, you can use a Gosu block to implement the interface. The Gosu block is an alternative to using an explicit anonymous class. You can use a Gosu block for interfaces that are defined in either Gosu or Java. For example:
_callbackHandler.execute(\ -> { /* Your Gosu statements here */ })
Enhancements
Gosu provides a feature called enhancements, which supports adding functions and properties to other types. Enhancements are especially powerful for enhancing native Java types, and types defined in other people’s code. For example, Gosu includes built-in enhancements on collection classes and interfaces, such as java.util.List
, that improve power and readability of collections code. For example, the following code takes a list of String objects, and uses a block to sort the list by the length of each String. The code then uses another block in the iteration across the result list to print each item:
strings.sortBy(\ str -> str.length).each(\ str -> print(str))
The sortBy
and each
methods in this example are Gosu enhancement methods on the List
interface. Both methods return the result list, which makes them useful for chaining methods in series.
Gosu Classes and Properties
Gosu supports object-oriented programming using classes, interfaces and polymorphism. Also, Gosu is fully compatible with Java types, so Gosu types can extend Java types, or implement Java interfaces. At the top of a class file, use the package
keyword to declare the package of this class. A package defines a namespace. To import specific classes or package hierarchies for later use in the file, add lines with the uses
keyword. This is equivalent to the Java import
statement. Gosu supports exact type names, or hierarchies with the *
wildcard symbol:
uses gw.example.MyClass // Exact type
uses gw.example.queues.jms.* // Wildcard symbol * specifies a hierarchy
class ABC {
construct(id : String) {
}
}
var a = new ABC("My initialization string")
//Gosu improves on this basic pattern and introduces a standard compact syntax for property initialization during object creation.
//For example, suppose you have the following Gosu code:
var myFileContainer = new my.company.FileContainer()
myFileContainer.DestFile = jarFile
myFileContainer.BaseDir = dir
myFileContainer.Update = true
myFileContainer.WhenManifestOnly = ScriptEnvironment.WHEN_EMPTY_SKIP
//After the first line, four more lines contain the object variable name, which is repeated information.
//You can use Gosu object initializers to simplify this code to only a couple of lines:
var myFileContainer = new my.company.FileContainer() { :DestFile = jarFile, :BaseDir = dir,
:Update = true, :WhenManifestOnly = ScriptEnvironment.WHEN_EMPTY_SKIP }
//You can also choose to list each initialization on its own line, which uses more lines but is more readable:
var myFileContainer = new my.company.FileContainer() {
:DestFile = jarFile,
:BaseDir = dir,
:Update = true,
:WhenManifestOnly = ScriptEnvironment.WHEN_EMPTY_SKIP
}
Related Info
Gosu Language Tutorials
In addition to the resources from the official Gosu language project, Guidewire offers a variety of resources to help developers explore and learn GOSU.
Gosu Reference Guide
Guidewire supplies a full Gosu reference guide within our Documentation site. The Gosu Reference Guide covers Gosu Programming Language end-to-end from basics like operators and expressions, to advanced topics like how to work with XML and JSON in Gosu. You must be logged in to our Documentation site to access the guide below.
Gosu Education Courses
Our Guidewire Education team has also created both instructor-led and self-study courses to help you understand the fundamentals of using Gosu in Guidewire applications. There are two courses that represent a good starting point for developers who would like to learn Gosu: InsuranceSuite 10.0 and 9.0 Developer Fundamentals and Introduction to Object-Oriented Programming Using Gosu.