Introduction to EGL packages

Packages work like folders: they organize source files and prevent naming conflicts.

Technically speaking, a package is a named collection of related parts, but it can be easier to think of a package in the same way as a folder or directory on your local system.

Packages prevent name conflicts by separating files into different contexts. Two parts with the same name cannot be defined in the same package, but two different packages can each have a part with the same name. Likewise, you should prevent conflicts between packages by not creating packages with the same name, even if those packages are in different projects or source folders.

The parts in an EGL source file all belong to the same package. The package statement in the file, if any, specifies the name of that package. If you do not specify a package statement, the parts are stored in the root of the source folder and are considered to be in the default package. Because files in the default package cannot be shared by parts in other packages or projects, it is best to specify a package statement.

Package names are case sensitive. For more information on naming conventions for packages, see package.

Working with packages

When you want a part to reference another part that is in the same package, you do not need to specify the location of the second part. The following example shows two parts in the same package: a Record part and a Program part that uses the record part:
package com.companyb.firstpackage;

program testProgram type BasicProgram
    function main()
        myVariable myRecordPart;
    end
end

Record myRecordPart type BasicRecord
    field1 int;
    field2 string;
end
When you want a part to reference another part that is in a different package, specify the complete location of the part within its package. For example, the following Program part uses the Record part from the previous example:
package com.companyb.secondpackage;

program testProgram2 type BasicProgram
    function main()
        myVariable2 com.companyb.firstpackage.myRecordPart;
    end
end

As shorthand, you can use the import statement to tell EGL that you want to use the part in the source file. If you import a part in this way, you can use the part as though it were in the current package, without specifying the complete location of the part within its package each time you use it. Sometimes, importing a part in this way is called "bringing the part into scope."

For example, the following Program part again uses the Record part defined earlier, but this time it imports the part first:
package com.companyb.thirdpackage;

import com.companyb.firstpackage.myRecordPart;

program testProgram3 type BasicProgram
    function main()
        myVariable3 myRecordPart;
    end
end
Note that the import statement uses the package path to the part and the part name, not the source file name.

For more information on import, see Import and use statements.


Feedback