Class variables and properties

Gosu supports instance variables and static variables in class declarations in basically the same way Java does, although the syntax is slightly different. Use the var keyword in the class definition, and declare the type explicitly. Note that variables are private by default in Gosu.

var _id : String // Variables are private by default

One difference between Gosu and some languages, including Java, is full support in Gosu for properties. A property provides a dynamic getter method and a dynamic setter method for its value. To set or get properties from an object, use natural syntax. Type the period (.) character followed by the property name just as for an object variable:

var s = myobj.Name
myobj.Name = "John"

Internally, Gosu calls the property getter and setter methods. In addition, Gosu has a special null-safety behavior with pure property paths, which have the form obj.Property1.Property2.Property3.

Define a property accessor function, which is a property getter, by using the declaration property get instead of function. Define a setter function by using function declaration property set instead of function. These property functions dynamically get or set the property, depending on whether the definition is property get or property set. Properties can be read/write, read-only, or write-only. Gosu provides a special shortcut to expose internal variables as public properties with other names. Use the syntax as PROPERTY_NAME in the definition for a variable. This syntax separates the internal implementation of the variable from how you expose the property to other code:

var _name : String as Name // Exposes the _name field as a readable and writable 'Name' property

This syntax is a shortcut for creating a property get function and a property set function for each variable. This syntax is the standard and recommended Gosu style for designing public properties. Do not expose actual class variables as public, although for compatibility with Java, Gosu does support exposing these variables.

The following code defines a simple Gosu class:

package example                 // Declares the package (namespace) of this class

class Person {

  var _name : String as Name    // Exposes the _name field as a readable and writable 'Name' property
  var _id : String              // Variables are private by default

  // Constructors are like functions called construct but omit the function keyword.
  // You can supply multiple method signatures with different numbers or types of arguments
  construct(id : String){ 
    _id = id
    _name = ""
  } 

  property get ID() : String {  
    // _id is exposed as a read only 'ID' property
    return _id
  }

  // Comment out the property set function to make ID a read-only property:
  property set ID(id : String) { 
    _id = id
  }

  // Functions are public by default
  function printOut() {
    print(_name + ":" + _id) 
  }
}

With this class, you can use concise code like the following:

n.ID = "12345"     // Set a property
print(n.ID)        // Get a property
n.Name = "John"    // Set a property -- See the "as Name" part of the class definition!
print(n.Name)      // Get a property -- See the "as Name" part of the class definition!

See also