Jamie Balfour

Welcome to my personal website.

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

Part 3.3Objects and structures in YASS

Part 3.3Objects and structures in YASS

Very early into the development of what was ZenLang and ZPE objects were introduced. Since their advent, objects have become much more important in ZPE. Structures were also added very early on and were in fact added way back in 2015 as one of the first major updates to the design of ZPE applications.

Both structures and objects play a major role in ZPE and are very important to the design of good applications.

Further to that, ZPE is a procedural language with object oriented concepts that extend it and will keep moving to a more object oriented manner as time goes on.

Objects in ZPE

An object represents a real world object, person or thing. Objects are often also known as entities in their development.

ZPE makes objects first-class citizens and like other languages like JavaScript, objects are composed of methods and properties. Methods are used to retrieve and change states of objects or perform a specific function and properties are used to retain information about the state of the object.

Since an object is a first-class citizen, it can be passed as an argument to a function call.

Objects are the base class of many programming languages, but in ZPE, being an interpreted language, objects are only the base class for structures. In ZPE, objects are often described as being structures without structure since they are identical to structures in the way they are both stored and represented but objects can have properties added to them at any time.

In ZPE, objects are created using the same syntax as JavaScript/JSON and use {} to enclose properties:

$person = {firstName : "Jamie", secondName : "Balfour"}

This object contains two properties: firstName and secondName.

Object properties can be accessed using the reference pointer: ->:


Structures in ZPE

A structure is actually a blueprint for an object. Structures allow for objects with a certain set of properties and methods to be created over and over again very quickly. In many other languages structures are known as classes, so in ZPE a structure may also be known as a class.

Structures when they are instantiated using new keyword become object instances. An object instance is an object that has been created from a structure (also known as the prototype in ZPE). Those object instances can be stored in variables, passed to functions as arguments etc and are just ordinary objects.

Structures can have public and private properties within them.

The following example creates a very simple structure and then instantiates it in the main function:

structure Car

  private $make = ""
  private $model = ""

  public function setMake($m)
    this->$make = $m
  end function

  public function getMake()
    return this->$make
  end function

  public function setModel($m)
    this->$model = $m
  end function

  public function getModel()
    return this->$model
  end function

end structure

function main()

  $car1 = new Car()
  $car2 = new Car()
  $car3 = new Car()



  $car3->setModel("Model S")

  print($car1->getMake() & " " & $car1->getModel())
  print($car2->getMake() & " " & $car2->getModel())
  print($car3->getMake() & " " & $car3->getModel())

end function

The Car structure is used to prototype the features about a car. It contains just two properties; $make and $model. However, it also includes several methods (methods in ZPE are functions within an object).

However, as a structure, none of the properties have been set within it until it is instantiated. To instantiate it, the new keyword is used to create an object instance and it is assigned to a variable.

Finally, the structure also contains several methods that can access the variables within it called setMake, getMake, setModel and getModel. These are called setter and getter methods or mutator and accessor methods.

Within the methods of the Car structure, there is the this keyword being used. This is what is known as a reference variable that refers to the current object context. When these methods are run, the current object context will be set to that object instance of the Car structure, so using this refers to that Car object instance when it is used.

Feedback 👍
Comments are sent via email to me.