Instructions

  • Write a small module with classes you can call from a Prolix program.
  • Import and use your new module.
  • Add simple error handling.
  • Start running your program and see the results.

Create a module

Let's start creating the module by creating a new file with the extension .prlx and to run the module you must also have a main file used to run the Prolix program.

This will be a small example of how to create a module that gives us a class that can run an attribute that can greets.

Now we will create a module named greets.prlx and first we will create a class in the module.

class greets;

And to create an active attribute, we will create a special attribute for the class called __edit__ and it will be activated when an attribute of the object is edited or affected then it will store the keyword of the modified attribute in __key__.

class greets :__edit__ {
    $math:eq $greets:__key__ "hello"; # Checking if edited attribute name's "hello"
    if $math:result {
        # "hello" attribute code here..
    }
};

Once completed, we can create an object with the same name (or any name) so that when we import, we will get a pre-defined object from the module you imported.

class greets :__edit__ {
    $math:eq $greets:__key__ "hello";
    if $math:result {
        $io:write "Hello, " :write $greets:hello :write "!\n";
    }
};
greets $greets; # Create a new predefined object for module

Importing module

Now to run the module you must use the keyword require to import the module (This feature cannot be used in Prolix 2.0.0).

require greets;

If your module is in a different directory or location, you can use require to import a module from the path.

require "./greets.prlx";

After importing the module is complete, everything in the module will be saved in the main program file.

require greets;
$greets:hello "Prolix";

Expected output:

Hello, Prolix!

Handle an error

Now we will add a new feature to detect errors and add errors to your module. Now suppose the "hello" attribute in the greets class must be a string and if not, an error will be reported.

class greets :__edit__ {
    $math:eq $greets:__key__ "hello";
    if $math:result {
        $utils:type $greets:hello; # Get the attribute value type
        $math:ne $utils:result "str"; # Check if the attribute value type is not a string
        if $math:result {
            $utils:error "name must be string"; # Report error
		}
        $io:write "Hello, " :write $greets:hello :write "!\n";
    }
};
greets $greets;

Now we'll try editing the "hello" attribute to a number to see what it looks like:

require greets;
$greets:hello 123;

Expected output:

C:/Users/Admin/Desktop/greets.prlx:7: name must be string
  from C:/Users/Admin/Desktop/greets.prlx:1