Defining properties

Standard Gosu style is to use public properties backed by private variables instead of using public variables.

Do not expose public variables in your classes, such as the following:
public var FirstName : String // Incorrect
Instead, write your Gosu classes to declare properties similar to the following line. This concise code defines the FirstName property and stores its value in the _firstname variable:
private var _firstName : String as FirstName

Gosu variable alias syntax provides this shortcut to declare an instance variable as a property. Variable alias syntax uses the as keyword followed by the property name to make simple getter and setter property methods backed by an class instance variable. Use this approach to specify a direct mapping between a publicly visible property and the private variable that contains the property's value. A property defined in this way is both readable and writable.

Although the data is stored in private variable _firstName, code that accesses this data does not access the private instance variable directly. Any code that needs to use the data uses the period symbol (.) and the property name:
var e = new Employee()
e.FirstName = "Pam" // Sets to "Pam" by calling the property setter
var g = e.FirstName // Calls the property getter

Code within a class does not need to use the property name to access a property on the same class. Classes can access their own private variables. In the previous example, other methods in that class could reference the _firstName variable rather than using the property accessor FirstName.

Writing property getter and setter methods explicitly

If you need to perform additional processing before storing or retrieving property values, you can write either or both of the getter and setter methods for the property. This form of a property definition is like a method that uses the keywords property get or property set before the method name instead of function. The property getter method must take zero parameters and the property setter method must take exactly one parameter.

Your property getter and setter methods can perform complex calculations or store the data in some other way than as a variable.

For example, the following code makes sure that the FullName, FirstName, and LastName properties are consistent in the setter methods for the FirstName, and LastName properties. The code also checks whether a user is authorized to access a social security number in the getter and setter methods for the SSN property.
package doc.example

uses gw.api.util.StringUtil

class Employee {
  var _fullName : String as readonly FullName
  var _firstName : String as FirstName
  var _lastName : String as LastName
  var _ssn : String as SSN

  construct(name : String) {
    setAllNames(name)
  }

  property get SSN() : String {
    return (isUserAuthorized() ? _ssn : "Not authorized to see SSN")
  }

  property set SSN(ssn : String) {
    if (isUserAuthorized()) {
      _ssn = ssn
    } else {
      print("Not authorized to set SSN")
    }
  }

  property set FirstName(fName : String) {
    setAllNames(fName + " " + _lastName)
  }

  property set LastName(lName : String) {
    setAllNames(_firstName + " " + lName)
  }

  private function setAllNames(name : String) {
    var allNames = StringUtil.splitWhitespace(name)

    _firstName = allNames[ 0]
    _lastName  = allNames[allNames.Count - 1]
    _fullName  = _firstName + " " + _lastName
  }

  private function isUserAuthorized() : boolean {
    // Dummy method
    return false
  }
}
The following code demonstrates the name and social security number properties:
var emp = new Employee("Pam Trujillo")
emp.SSN = "123456789"
print("First name:\t${emp.FirstName}")
print("Last name:\t${emp.LastName}")
print("Full name:\t${emp.FullName}")
print("SSN:\t${emp.SSN}")
This code produces the following output:
Not authorized to set SSN
First name:	Pam
Last name:	Trujillo
Full name:	Pam Trujillo
SSN:	Not authorized to see SSN

Read-only and write-only properties

By default, properties are both readable and writable. You can make a property read-only by adding the keyword readonly before the property name, as shown in the following code:
var _fullName : String as readonly FullName

Gosu does not provide a keyword to make a write-only property in this shortcut syntax.

If you need to use getter and setter methods to perform additional processing for the property, you make a read-only property by writing only the getter method. You use a different code path, such as the class constructor, to set the value of the property. Similarly, you can make a write-only property by writing only the setter method, as shown in the following code:
var _secretID : int

property set SecretID(max : int) {
  _secretID = (Math.random() * max) as int
}