Create a structural type with getters, setters, and methods

About this task

To create a structural type, you perform the following general steps:

Procedure

  1. In the package for your new type, create a Gosu class file with a .gs file extension.
  2. In the Gosu editor, change the word class to structure.
  3. Just as you would declare on an interface, add property declarations and method signatures:
    • To provide data from a variable or property on an instance of this type, use property getter syntax:
      property get PROPERTY_NAME() : TYPE
    • To set the value of a variable or property on an instance of this type, use property setter syntax:
      property set PROPERTY_NAME(TYPE)
    • To provide a callable method on an instance of this type, use the syntax:
      public function METHOD_NAME(ARG_LSIT) : TYPE

Example

package docs.example

structure DemoStructure{
  property get Name() : String
  property set Name(n : String) 
  public function count(s : String) : double
}

This example declares a structural type called DemoStructure that has the following features:

  • Name property of type String
  • count method, which takes one String parameter and returns a double value

What to do next

See also