Classes

A class is a declaration of a constructor of objects with a specific type. Each object has zero or more attributes (instance variables) with zero or more behaviours (methods), that are defined inside a specific class or inherited from super-classes.

class Person (name, age, address) {
    method position {
        # GPS.locate(self.address)
    }

    method increment_age(amount=1) {
         self.age += amount
    }
}

var obj = Person(
       name: "Foo",
        age: 50,
    address: "St. Bar"
)

say obj.age                # prints: 50
say obj.name               # prints: "Foo"
say obj.address            # prints: "St. Bar"

obj.name = "Baz"           # changes name to "Baz"
say obj.name               # prints: "Baz"

obj.increment_age          # increments age by 1
say obj.age                # prints: 51

Class attributes

The attributes of a class can be either specified as parameters, or declared with the has keyword.

Class initialization

Extra object-initialization setup can be done by defining a method named init, which will be called automatically called whenever a new instance-object is created.

Class variables

The syntax ClassName!var_name can be used for defining, accessing or modifying a class variable.

The modification of a class variable can be localized by prefixing the declaration with the local keyword:

Class inheritance (experimental)

Inheritance of behaviors and attributes, by a given class, is declared with the < operator, followed by the name of the class from which the current class inherits:

Multiple inheritance is declared with the << operator, followed by two or more class names, separated by commas:

Last updated

Was this helpful?