PreviousUpNext

5.2.10  Packages

Mythryl packages are a bit like C++ classes or namespaces. They let you construct a hierarchy of namespaces so that not every variable need be dumped into the global namespace as in C. Like C++, Mythryl uses the package::function syntax to access a function within an external package:

    #!/usr/bin/mythryl

    # Define a simple package:
    #
    package my_package {
        fun double(i) = { 2*i; };
    };

    # Invoke the function in the package:
    #
    printf "%d\n" (my_package::double(2));

    # Define a shorter name for the package:
    #
    package p = my_package;

    # Invoke the same function by the shorter package name:
    #
    printf "%d\n" (p::double(3));

    # Dump all identifiers in the package into
    # the current namespace:
    #
    include package   my_package;

    # Now we can call the same function
    # with no package qualifier at all:
    #
    printf "%d\n" (double(4));

When you run the above, it will print out

    linux$ ./my-script
    4
    6
    8
    linux$

We shall have a great deal more to say about packages later, but the above will do for the moment.

For our current purposes, writing small scripts for didactic and utilitarian purposes, we will not often have cause to create packages. We will be far more interested in taking advantage of various packages from the Mythryl standard library.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext