Skip to content

Class

A class definition is a special type of function that can be used to create a custom data-type. A class definition is not a variable, and it cannot be saved to a variable, it is your way of letting Modulo1 know how to create a type of custom variable. Once you have created the definition, Modulo1 will allow you to create variables that follow the structure that was specified in the definition. And those variables that are built based on the class definition will be referred to here as class objects

Class definitions are especially helpful in situations where you might have otherwise found yourself creating many repetitions of the same object structure. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
var bear = { 
    colour : "brown",
    weightKg : 300,
    printInfo : function() returning null {
        print("A grizzly bear is " + colour + ", and weighs " + weightKg + "kgs");
    }
};

var wolf = {
    colour : "grey",
    weightKg : 40,
    printInfo : function() returning null {
        print("A timber wolf is " + colour + ", and weighs " + weightKg + "kgs");
    }
};

Having only two of these objects (bear and wolf) is OK, but if the number of repititions increases, it will start to become a problem. The solution is to create a class definition instead. Here is how that would look:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Animal(string name, string colour, number weightKg) {
    public {
        function printInfo() returning null {
            print("A typical " + name + " is " + colour + ", and weighs " + weightKg + "kgs");
        };
    };
};

var bear = Animal("grizzly bear", "brown", 300);
var wolf = Animal("timber wolf", "grey", 40);

In this example, we begin with the keyword class. This tells Modulo1 that we're about to define a class' structure. Then we provide the name of our class definition, which is Animal, followed by a list of inputs that help Modulo1 to create a class object

Each input in the list has two words. The first word is the data-type that our class is expecting. And the second word is the identifier that our class intends to give to that input. Our Animal class is expecting a string as the first input and intends to identify it as name.

Next we find a public code block.

Info

You will find more information about class definition code blocks in the Syntax section below.

Inside the public code block we find a single function called printInfo(), which prints a single line of text. That to generate that line of text, the function makes reference to the three input variables name, colour, and weightKg.

That was enough information to create a very basic class definition which was then used to create two class object variables: bear and wolf. We could now easily create a third Animal like this:

1
var dog = Animal("pitbull", "white", 20);

Syntax

A class definition begins with the class keyword, followed by a list of variable input requirements. For example:

1
2
3
class Animal(string name, string colour, number weightKg) {

}

In this example, our class definition requires the following inputs:

  • A string, which it will refer to as name
  • Another string, which it will refer to as colour
  • And a number, which it will refer to as weightKg

A class can have three different types of code blocks, and the choice of code block has an affect on the code that is written inside the block.

  • Variables and functions inside a public block can be accessed from both inside and outside the class. They can be accessed by code that is within public and private blocks, but they cannot be accessed by code that is within a static block
  • Variables and functions inside a private block can only be accessed by code that is within the public and private blocks. They cannot be accessed by code that is within a static block, or from code that is outside the class
  • Variables and functions inside a static block are shared by all class object's that share the same definition, and can be accessed from both inside and outside the class. They can be accessed by public, private and static blocks

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Account(string name, number balance) {
    public {
        totalAccounts += 1;

        function getInfo() returning string {
            if (accNumber == null) {
                return "Bank account for " + name + " is invalid";
            } else {
                return "Bank account for " + name + " with $" + balance;
            }
        }
    }

    private {
        const accNumber = number.randomInt(10000, 100000000);
    }

    static {
        var totalAccounts = 0;
    }
}

var acc1 = Account("Fred Flinstone", 1002);
var acc2 = Account("Marge Simpson", 505);

In this example, we have a public, a private, and a static code block. And we have created two class objects: acc1 and acc2. But before it created those objects, Modulo1 had already run the static code block. Since the static code is shared by all Account class objects, there is no need for Modulo1 to run the code within that block repeatedly. So, this shared variable called totalAccounts had a value of 0.

Then, when we created acc1, Modulo1 ran the public code block, which achieved two things: 1. It increased the totalAccounts shared variable to 1 2. It gave acc1 access to the function getInfo() and made that function available from the outside.

What this means, is that Modulo1 will allow us to to write the following code:

1
var result = acc1.getInfo();

This is because getInfo() is a public function.

However, when we created acc1, Modulo also ran the private code block, which created a constant (i.e. unchangeable) variable called accNumber. This variable can be used by the getInfo() function, but we cannot access that variable using any code that is written outside the class. For example, the following code will cause an error:

1
2
var acc1 = Account("Fred Flinstone", 1002);
var result = acc1.accNumber;

Finally, when our example above creates the acc2 variable, its public code block increased the totalAccounts shared variable to 2. Remember, this variable is shared by both acc1 and acc2 so that the following two lines will print the exact same result:

1
2
print(acc1.totalAccounts);
print(acc2.totalAccounts);

We can also access our static variable directly, through the class definition name:

1
print(Account.totalAccounts);

To access a variable that is within a class object:

1
var result = bear.name;

An alternative approach will yield the exact same outcome:

1
var result = bear["name"];

Properties

A property is a special feature of a variable that can be used to get and/or set some aspect of that variable.

type

The type property can be used to find out the name of the class definition that was used to create a particular class object

1
var result = myClassObject.type;

Functions

There are no functions specific to class definitions or class objects.