Package Access

1 General Description

There are essentially two ways of accessing the definitions of a package either direct reference by prefixing the package name to the definition names or importing definitions from the package. We have already used the first method in the Complex Example. It is usually more convenient and often necessary to use the import mechanism, which is the subject for the next section.

2 Importing Definitions from a Package

Import statements in a package or class fill the two needs of either making definitions from other packages available for use in a package or class or explicit declaration of usage dependences on other packages. An import statement can occur in several syntactic forms. In the examples below we want to access the Add operation of the package Modelica.Math.ComplexNumbers.

2.1 Qualified Import

The qualified import statement of the form import ; imports all definitions in a package, which subsequently can be referred to by names simplepackagename.definitionname, where the simplepackagename is the packagename without its prefix. This is the most common form of import (see ComplexUser1) that eliminates the risk for name collisions when importing from several packages.

2.2 Single Definition Import

The single definition import statement of the form import .; allows you to import a single specific definition from a package and use that definition referred to by its definitionname without the package prefix. This allows you to only write Add (see ComplexUser2 below) but still you have to write an import statement for each imported definition which can be a bit cumbersome. There is no risk for name collision as long as you do not try to import two definitions with the same short name.

2.3 Unqualified Import

The unqualified import statement of the form import packagename.*; imports all definitions from the package using their short names without qualification prefixes. This is shown in ComplexUser3 below. There is a high risk of name collisions as well as future compilation errors, but nice for the lazy programmer.

2.4 Renaming Import

The renaming import statement of the form import = ; imports a package and renames it locally to shortpackagename. You can refer to imported definitions using shortpackagename as a presumably shorter package prefix. In the class ComplexUser4 below, Co is used instead of the longer name ComplexNumbers.