Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Official ZPE/YASS documentationStructures and classes

A structure is a data type in which many inner values can be specified. It provides a framework for all variables and functions.

YASS
structure person

  $name = null
  $age = 0
  $email = null

  function set_values($n, $a, $e)
    $name = $n
    $age = $a
    $email = $e
  end function

end structure
  

Instead of the full word structure being used, the keyword struct may be used.

A structure must be declared using the new keyword (or before ZPE version 1.12.8 the copyof keyword also worked).

Variables and internal functions can be accessed using the object pointer literal, ->, as shown below:

YASS
function main($args)
  $t = new person()

  //Run an internal function within a structure
  $t->set_values("Jack", 20, "jack@example.com")
  //Obtain and print a variable from a structure
  print($t->$name)
  //Set a variable in a structure
  $t->$name = "Joseph"
end function
  

Prior to version 1.5.3, the object pointer literal was called the pointer literal and was represented by => (fat arrow). The fat arrow was moved to association using maps and associative arrays.

Special methods

Structures may also be constructed with parameters as of version 1.5.2 of ZPE. The important thing is that they have a function named _construct:

YASS
structure person

  $name = null
  $age = 0
  $email = null

  function _construct($n, $a, $e)
    $name = $n
    $age = $a
    $email = $e
  end function

end structure

function main($args)
  $p = new person("John", 20, "john@example.com")
end function
  

YASS
structure Person

  $name = null
  $age = 0
  $email = null

  function _output()
    return $name & " " & $age
  end function

end structure

  

Remember, in ZPE, structures are templates or prototypes for new objects and objects are the instantiated versions of these structures.

There are other special functions that can be used within an object such as the _output or the _compare functions:

YASS
structure Person

  $name = null
  $age = 0
  $email = null

  function _construct($n, $a, $e)
    $name = $n
    $age = $a
    $email = $e
  end function

  function _output()
      return this->$name & "(" & this->$email & ")"
  end function

  function _compare($o)
    return $o->$name == this->$name
  end function

end structure

function main($args)
  $p1 = new Person("John", 20, "john@example.com")
  $p2 = new Person("John", 48, "john2@example.com")

  //Will return true
  print($p1 == $p2)
end function
  

Version 1.6.4 of ZPE added ZPEObjectNativeFunctions as an option which allows functions defined within Java to add native Java-based function calls on objects.

Namespaces

ZPE 1.12.8 added support for namespaces to keep code much better organised.

Languages like Java use this to keep packages tidy and prevent bringing ambiguity into code. For example, assume a library by James Smith is imported that introduces a new structure called HTMLBuilder and the program was using ZPE's own HTMLBuilder structure. The new HTMLBuilder would overwrite the existing one. Instead of overwriting this, James could use a namespace on his structure:

YASS
structure HTMLBuilder

  namespace jsmith/packages

  /*
  * Functions go here
  * ...
  */

end structure

$builder = new jsmith/packages/HTMLBuilder()
  

Now when ever he needs to use James' HTMLBuilder, he would just use the full path of the structure.

Inheritance

A child structure inherits the accessible properties and methods of its parent. It may add new members or override inherited methods. Use either inherits or extends to name the parent structure.

YASS
structure Animal

  $name = null

  public function describe()
    return "Animal: " & this->$name
  end function

end structure

structure Dog inherits Animal

  public function describe()
    return super->describe() & " (dog)"
  end function

end structure

$dog = new Dog()
print($dog->describe())
  

When a child overrides a method, super->method() calls the inherited parent implementation. Arguments are passed normally and the parent method runs against the same child object, so this continues to refer to the child instance.

Lookup can continue through several parent levels. A private parent method cannot be called with super. Inheritance itself has existed in ZPE for a long time; its linked-parent implementation was substantially overhauled and super was introduced in ZPE 1.14.9 (Spurriergate, September 2026).

Static methods

A static method belongs to the structure itself rather than to objects created from that structure. Its implementation was made available in ZPE 1.14.9 (Spurriergate, September 2026). Declare one with the static keyword and call it using the scope-resolution operator, ::.

Public static methods are not copied to each object instance. They are useful for factory functions, shared lookups, and operations that do not require the properties of an individual object.

YASS
structure PersonObject

  public static function get_people_list()
    return ["Jamie", "Jack", "Joseph"]
  end function

end structure

$people = PersonObject::get_people_list()
    

A static method must be public to be called from outside its structure. Because no object is constructed for a static call, instance properties and this are not available inside the method.

The program shown below was generated by ChatGPT. It was taught nothing about the language other than the general syntax and it immediately produced valid code, showcasing how simplistic the inheritance pattern in YASS actually is.

YASS
// ZPE/YASS inheritance showcase
//
// This example deliberately exercises:
//   * several levels of inheritance
//   * method overriding and inherited methods
//   * super calls
//   * public static methods
//   * parent-typed variables containing child objects
//   * polymorphic lists and function parameters
//   * nominal runtime type checks

structure Animal

  public function identify()
    return "animal"
  end function

  public function family()
    return "Animal"
  end function

  public function habitat()
    return "the natural world"
  end function

  public function diet()
    return "an unspecified diet"
  end function

  public function act()
    print("The animal is acting")
  end function

  public function communicate()
    return "an animal sound"
  end function

  public function movement()
    return "moves"
  end function

  public function description()
    return this->identify() & " belongs to " & this->family()
      & ", lives in " & this->habitat()
      & ", " & this->movement()
      & " and eats " & this->diet()
  end function

  public static function alertMe()
    print("Alert from Animal!")
  end function

end structure


structure Mammal inherits Animal

  public function identify()
    return "mammal"
  end function

  public function family()
    return "Mammalia"
  end function

  public function movement()
    return "walks or runs"
  end function

  public function careForYoung()
    return "nurses its young"
  end function

end structure


structure Canine inherits Mammal

  public function identify()
    return "canine"
  end function

  public function family()
    return "Canidae"
  end function

  public function communicate()
    return "a canine call"
  end function

end structure


structure Dog inherits Canine

  public function identify()
    return "dog"
  end function

  public function habitat()
    return "a human home"
  end function

  public function diet()
    return "dog food"
  end function

  public function act()
    print("The dog fetches a ball")
  end function

  public function communicate()
    return "woof"
  end function

  public function inheritedIdentity()
    return super->identify()
  end function

end structure


structure WorkingDog inherits Dog

  public function identify()
    return "working dog"
  end function

  public function act()
    print("The working dog completes its task")
  end function

  public function parentAction()
    super->act()
  end function

end structure


structure GuideDog inherits WorkingDog

  public function identify()
    return "guide dog"
  end function

  public function act()
    print("The guide dog safely leads its handler")
  end function

  public function role()
    return "mobility assistance"
  end function

end structure


structure Fox inherits Canine

  public function identify()
    return "fox"
  end function

  public function habitat()
    return "woodland"
  end function

  public function diet()
    return "small animals, insects and fruit"
  end function

  public function act()
    print("The fox explores quietly")
  end function

  public function communicate()
    return "a bark-like call"
  end function

end structure


structure Feline inherits Mammal

  public function identify()
    return "feline"
  end function

  public function family()
    return "Felidae"
  end function

  public function communicate()
    return "a feline call"
  end function

end structure

structure BigCat inherits Feline

  public function identify()
    return "big cat"
  end function

  public function movement()
    return "stalks and prowls"
  end function

  public function communicate()
    return "a deep feline call"
  end function

end structure


structure Cat inherits Feline

  public function identify()
    return super->identify()
  end function

  public function habitat()
    return "a human home"
  end function

  public function diet()
    return "cat food"
  end function

  public function act()
    print("The cat climbs onto a shelf")
  end function

  public function communicate()
    return "meow"
  end function

end structure


structure Lion inherits BigCat

  public function identify()
    return "lion"
  end function

  public function habitat()
    return "grassland"
  end function

  public function diet()
    return "meat"
  end function

  public function act()
    print("The lion patrols its territory")
  end function

  public function communicate()
    return "roar"
  end function

end structure

structure MountainLion inherits BigCat

  public function identify()
    return "mountain lion"
  end function

  public function habitat()
    return "mountains and forests"
  end function

  public function diet()
    return "deer and other prey"
  end function

  public function act()
    print("The mountain lion moves silently through the forest")
  end function

  public function communicate()
    return "a piercing scream"
  end function

end structure


structure Jaguar inherits BigCat

  public function identify()
    return "jaguar"
  end function

  public function habitat()
    return "tropical rainforest"
  end function

  public function diet()
    return "fish, reptiles and mammals"
  end function

  public function act()
    print("The jaguar stalks prey near the river")
  end function

  public function communicate()
    return "a rasping roar"
  end function

end structure


structure Panther inherits BigCat

  public function identify()
    return "panther"
  end function

  public function habitat()
    return "dense forest"
  end function

  public function diet()
    return "meat"
  end function

  public function act()
    print("The panther slips through the shadows")
  end function

  public function communicate()
    return "a low growl"
  end function

end structure


structure Bird inherits Animal

  public function identify()
    return "bird"
  end function

  public function family()
    return "Aves"
  end function

  public function movement()
    return "flies"
  end function

  public function layEgg()
    return "lays an egg"
  end function

end structure


structure Eagle inherits Bird

  public function identify()
    return "eagle"
  end function

  public function habitat()
    return "mountains and open country"
  end function

  public function diet()
    return "fish and small animals"
  end function

  public function act()
    print("The eagle circles high overhead")
  end function

  public function communicate()
    return "a high-pitched call"
  end function

end structure


structure Penguin inherits Bird

  public function identify()
    return "penguin"
  end function

  public function habitat()
    return "the cold southern coast"
  end function

  public function diet()
    return "fish"
  end function

  public function movement()
    return "swims and waddles"
  end function

  public function act()
    print("The penguin dives into the water")
  end function

  public function communicate()
    return "a braying call"
  end function

end structure


function inspectAnimal(mixed $animal)
  print("Identity: " & $animal->identify())
  print("Family: " & $animal->family())
  print("Sound: " & $animal->communicate())
  print("Summary: " & $animal->description())
  $animal->act()
  print("")
end function


function printTypeChecks(mixed $animal)
  print("Animal: " & ($animal is type of Animal))
  print("Mammal: " & ($animal is type of Mammal))
  print("Canine: " & ($animal is type of Canine))
  print("Dog: " & ($animal is type of Dog))
  print("Bird: " & ($animal is type of Bird))
  print("Runtime type: " & type($animal))
end function


print("=== Parent references and dynamic dispatch ===")

Animal $animal = new Dog()
$animal->act()
print($animal->identify())
print($animal->communicate())

Animal $animal2 = new Cat()
print($animal2->identify())

Animal $animal3 = new Fox()
print($animal3->identify())
$animal3->act()

Animal::alertMe()
print("")


print("=== Calls through several inheritance levels ===")

Dog $dog = new Dog()
print("Dog identifies its parent as: " & $dog->inheritedIdentity())

WorkingDog $worker = new WorkingDog()
$worker->act()
$worker->parentAction()

GuideDog $guide = new GuideDog()
print($guide->identify() & " performs " & $guide->role())
$guide->act()
print("")


print("=== Polymorphic sanctuary collection ===")

$residents = [
  new Dog(),
  new GuideDog(),
  new Fox(),
  new Cat(),
  new Lion(),
  new MountainLion(),
  new Jaguar(),
  new Panther(),
  new Eagle(),
  new Penguin()
]

for each($residents as $resident)
  inspectAnimal($resident)
end for


print("=== Nominal type checks ===")

printTypeChecks($animal)
print("")

print("Fox is Animal: " & ($animal3 is type of Animal))
print("Fox is Mammal: " & ($animal3 is type of Mammal))
print("Fox is Canine: " & ($animal3 is type of Canine))
print("Fox is Dog: " & ($animal3 is type of Dog))

$penguin = new Penguin()
print("Penguin is Animal: " & ($penguin is type of Animal))
print("Penguin is Bird: " & ($penguin is type of Bird))
print("Penguin is Mammal: " & ($penguin is type of Mammal))

$jaguar = new Jaguar()

print("Jaguar is Animal: " & ($jaguar is type of Animal))
print("Jaguar is Mammal: " & ($jaguar is type of Mammal))
print("Jaguar is Feline: " & ($jaguar is type of Feline))
print("Jaguar is BigCat: " & ($jaguar is type of BigCat))
print("Jaguar is Lion: " & ($jaguar is type of Lion))
print("Runtime type: " & type($jaguar))


Comments

There are no comments on this page.

New comment

Comments are welcome and encouraged, including disagreement and critique. However, this is not a space for abuse. Disagreement is welcome; personal attacks, harassment, or hate will be removed instantly. This site reflects personal opinions, not universal truths. If you can’t distinguish between the two, this probably isn’t the place for you. The system temporarily stores IP addresses and browser user agents for the purposes of spam prevention, moderation, and safeguarding. This data is automatically removed after fourteen days. Your email address is stored so that replies can be sent to your email address.

Comments powered by BalfComment

Feedback 👍
Comments are sent via email to me.