Pirates of the JVM series

Gosu — Hey look! It’s a pragmatic language for the JVM. A simple language

JAX Editorial Team
Gosu

Finding the island of Gosu is not an easy thing to do. According to the legends of the Pirates of the JVM, the mystical island of Gosu is a chameleon when it comes to staying in one lane. What makes Gosu so unique? Is it really a *simple* JVM language? We talked to Scott McKinney, the language’s father, about its advantages and disadvantages, its core principles and more.

This year, Gosu celebrates its 15th anniversary. This pragmatic programming language’s set of features allows Java developers to be more productive without sacrificing the benefits of static typing. In short, Gosu is an object-oriented language with a sprinkle of functional programming features.

What separates Gosu from other JVM languages? How did its development begin? Lucky for us, we get to hear all about this simple JVM language from its creator.

Click the image to see the whole Pirates of the JVM infographic.

Gosu — Background information

JAXenter: What was your motivation for inventing Gosu? What does Gosu have to offer that other languages fail to provide?

Scott McKinney: Gosu was not “created” per se, rather it is a result of a long and ongoing evolutionary process. The impetus for Gosu was my employer’s need for a statically typed scripting language to enable our customers to drive and customize our suite of applications.  There were no practical, statically typed scripting languages available in that period (2002); that is pretty much what got the ball rolling.  And today despite its large feature set and general purpose status, Gosu remains a powerful, concise scripting language mostly afforded by sophisticated type inference and optional compilation.  The latter feature, optionally executing directly from source is a function of Gosu’s “open type system”, which is at the heart of what makes Gosu truly unique among other languages.

Gosu is the result of a long and ongoing evolutionary process.  

Basically, the open type system is about abstracting away the notion of a metatype so that 3rd parties can provide custom types directly and seamlessly to Gosu developers. By “metatype” I mean the langauge’s type. For instance, Java’s metatype is the java.lang.Class type.  It is fixed in Java’s type system; there is no other way to expose a type to Java’s type system. As such Java developers typically write code generators or indirect libraries to map a specific type domain to Java.  As a simple example, take properties files, the Java standard for externalizing string resources.  Here we have a simple properties file, MyProperties.properties, in the “abc” Java package:

# MyProperties.properties
chocolate=Chocolate
chocolate.dark=Dark Chocolate
chocolate.milk=Milk Chocolate

Since there is no type-safe access to this data, Java provides the utility class, java.util.Properties, as the standard means to access properties in your code:

var prop = new Properties()
using(var stream = Class.getResource("/abc/MyProperties.properties").openStream()) {
  prop.load(stream)
}
print(prop.getProperty("chocolate.dark"))

As you can see this code is verbose and not type-safe. With Gosu’s open type system the Properties type domain is exposed directly to developers:

print(abc.MyProperties.chocolate.dark)

This code is type-safe and works seamlessly with static code analysis tooling, including IDE code completion. Basically, in this example, a custom type plugin uses Gosu’s open API to expose each property file resource as a static type. There is no code generation involved and no user intervention required to leverage the type plugin.

In addition to properties, we provide custom type support for XSD/XML, SQL/DDL, JSON, JavaScript, and others are in the works.

A host of smaller features that set Gosu apart includes Binding Expressions. Read more here.

JAXenter: Can you describe the core principles of the language?

Scott McKinney: Gosu was gradually crafted and field tested over time to meet direct usability and performance requirements of our large-scale enterprise products involving many thousands of developers, resulting in millions of lines of production code.

We realized early on that static typing would be a key element of our language; deterministic static analysis tooling such as instant editor feedback, code completion, code navigation, feature usage, refactoring, etc. would otherwise not be feasible.  Without this level of tooling our developers and our customers would be at a major disadvantage.  To make static typing more consumable we also realized type inference would play a huge role in making Gosu more concise and scriptable.

We realized early on that static typing would be a key element of Gosu.  

On a more basic level, we wanted Gosu to be familiar, approachable, and easy to learn for most programmers.  To that end Gosu at its core is a relaxed, imperative/OOP language; for instance, Java programmers have no trouble moving on to Gosu.

To be more productive, however, we also understand that Gosu must leverage modern ideas and borrow from other languages that have successfully employed them.  These include functional programming, type enhancements, dynamic features, reified generics, and structural typing to name just a few.  Essentially, we want Gosu to empower developers, but not overwhelm them with complicated or impractical features.

“Java programmers have no trouble moving on to Gosu”

JAXenter: What would a typical program with Gosu look like?

Scott McKinney: Gosu is a general-purpose language supporting a wide variety of OOP types, these include Classes, Enumerations, Interfaces, Structures, Annotations, Enhancements, Programs, Templates, Functions, and Custom Type plugins.  Program types are Gosu’s more practical answer to Java’s Main class idiom.  A Program file is a sort of free-form scripting canvas where you can define top-level variables, statements, and classes in any arrangement you like.  A Program is also a first-class type, just like a Class.  For instance, you can reference a Program by name and execute it programmatically.   

Here’s a simple example demonstrating some these features.  This file, Plasma.gs, is directly loadable and executable as first-class type, Plasma:

uses javax.swing.*
uses java.awt.*
uses java.awt.image.*
uses java.awt.event.ActionEvent
uses java.awt.image.BufferedImage#*
uses java.lang.Math#*
 
var size = 400
EventQueue.invokeLater(\ -> showPlasma())
 
function showPlasma() {
  var frame = new JFrame("Plasma") {:Resizable = false, :DefaultCloseOperation = JFrame.EXIT_ON_CLOSE}
  frame.add(new Plasma(), BorderLayout.CENTER)
  frame.pack()
  frame.setLocationRelativeTo(null)
  frame.Visible = true
}
 
class Plasma extends JPanel {
  var hueShift: float
  property get plasma: float[][] = createPlasma(size, size)
  property get img: BufferedImage = new BufferedImage(size, size, TYPE_INT_RGB)
 
  construct() {
	PreferredSize = new Dimension(size, size)
	new Timer(50, \ e -> {hueShift+=0.02 repaint()}).start()
  }
 
  private function createPlasma(w: int, h: int): float[][] {
	var buffer = new float[h][w]
	for(y in 0..|h)
  	for(x in 0..|w) {
        var value = sin(x / 16) + sin(y / 8)
          + sin((x + y) / 16) + sin(sqrt(x * x + y * y) / 8) + 4
        value /= 8
        assert value >= 0 && value <= 1 : "Hue out of range"
        buffer[y][x] = value as float
  	}
	return buffer
  }
 
  override function paintComponent(g: Graphics) {
	for(y in 0..|plasma.length)
  	for(x in 0..|plasma[0].length)
        img.setRGB(x, y, Color.HSBtoRGB(hueShift + plasma[y][x], 1, 1))
	g.drawImage(img, 0, 0, null)
  }
}

This example demonstrates how you can define top-level variables, functions, and classes in a single file to form a program or script. Note that you can use Gosu’s compiler to compile this program to a conventional .class file or you can run it directly from source – this capability makes Gosu ideal for scripting.

JAXenter: For what kind of applications/use cases is Gosu well-suited? For which ones it is not?

Scott McKinney: Gosu is designed to scale across the full spectrum of project sizes, from small scripts to million line enterprise applications. You can use Gosu anywhere you can use a standard Java VM.  You can build large-scale projects with Gosu using the Gosu IntelliJ plugin, which supports the full breadth and depth of IntelliJ features.  Or, you can use Gosu as a powerful and concise scripting language.  Gosu applications are inherently scriptable because Gosu can load directly from source – there’s even an eval() construct built into the language. In addition, Gosu directly integrates with Javascript in the type system and supports JSON directly.

You can use Gosu anywhere you can use a standard Java VM.

Gosu is currently not suitable for Android application development. We are currently discussing a future roadmap for this use-case.

JAXenter: What is the current state of the language?

Scott McKinney: Gosu is a mature language. It is used heavily in large-scale enterprise applications since 2003. There are currently many millions of lines of operational Gosu code running across the globe.  Gosu is also fully open source.

JAXenter: How about your plans?

Scott McKinney: The Gosu team continues to grow and innovate. A separate team is currently upgrading the Gosu IntelliJ plugin, adding a lot of cool new features. We are also working on a learning platform with our own home grown IDE (Gosu Lab) targeting academia and small projects.

Several projects are in the works: gosu-web, gosu-js, and gosu-db.  These all fall under the new Gosu Platform project.

Of course, we are always working to improve core gosu-lang.  I’ve blogged recently about some of the newer features.

JAXenter: How can people interested in Gosu get started?

Scott McKinney: Visit our website and definitely check out the Getting Started section. We are currently in the process of redesigning our website with a better user experience and a lot more content wrt learning Gosu.

 

Scott McKinney is a staff engineer at Guidewire where he designed and created Gosu. He currently leads the Gosu engineering team and pounds code by the truckload while listening to way too much retro synthwave.

 

Pirates of the JVM — The series:

Are you familiar with all the programming languages from the JVM universe? Have you discovered them all?

If the answer is “no”, we’ll leave no stone unturned in the search for knowledge. Therefore, the Pirates of the JVM infographic is accompanied by a series of articles in which we put the spotlight on the languages you may or may not know.

Don’t forget to check out the Pirates of the JVM series.

Comments
comments powered by Disqus