Skip to content

Libraries

Libraries

Morpho is a modular environment. A number of standard libraries are provided, and you can easily write and distribute your own. To use a library, use the import keyword

import color

When compiling this code, the Morpho compiler looks for a library called colorreally a file names color.morphoin a number of standard locations that depend on the platform and how Morpho was installed; the collection of search locations is called the environment. Morpho packages, bundles of code, help files, documentation etc. can be added to the environment by the user and provide a structured mechanism to extend its capabilities.

Once a library has been imported, anything it defines becomes available to the user: classes, functions and even global variables. These collectively are referred to as the symbols defined by the library. The color module provides a few named colors, a Color class, as well as ColorMap objects that map a scalar parameter to a range of colors (useful for plotting).

You can also load a Morpho file using import

import "mylibrary.morpho"

In this case, the compiler looks for the file "mylibrary.morpho" in the same folder as your code; it doesn't search the environment for the file. You can load files in subfolders using UNIX path syntax

import "folder/mylibrary.morpho"

If necessary, the Morpho compiler will translate the UNIX path into a platform specific descriptor.

Unlike in some languages, import can only be used in the global scope, but it can be used at any suitable point in the program. You may also import more than one library per line

import color, meshtools

and these are then imported in the order specified.

Importing selected symbols

Sometimes, the programmer only wants to use a subset of the features of a library. To do this, use the for keyword together with import to select which symbols to import. This statement imports a special color-accessible ColorMap from the color module

import color for ViridisMap

Note that no other symbols from color are imported, not even ColorMap from which ViridisMap inherits.

Judicious use of import ... for improves the robustness of code. As libraries are updated, new symbols may be added that could conflict with a symbol in the code using the library. By making only the components needed accessible, the possibility of conflicts is reduced.

Namespaces

Avoidance of conflicts between libraries motivates a more general construct called a namespace. These are containers for symbols defined at compile time; any library can be imported into a designated namespace using the as keyword, e.g.

import color as col

where col is the name of the namespace created to contain the color libraries definitions. Once a namespace has been defined, its contents can be accessed using the . operator, similar to a property lookup:

var c = col.Red()

A namespace lookup can be used anywhere where a regular identifier could be used, e.g. in constructors, function definitions and function calls, type specifiers, etc. The compiler attempts to locate the given the symbol in the namespace, and throws an error SymblUndfNmSpc if it cannot be found.

Multiple includes

Morpho will never load the same library twice, even if it is imported more than once, and even if it is imported using for or as as described below. Different import statements can even use the library in different waysMorpho will automatically ensure the correct symbols are available.