Package Usage and Development » Advanced Features
New-Style Packages
As of version 10 or so there is a new package format that may be used to more easily handle complicated package setups. It involves a set of static tokens that one uses to tell the system how to distribute definitions and expose contexts and things. These are:
Package— must be at the top of a package file, tells the system it's a package and the package namePackageExport— tells the system that a given symbol will be exposed at top levelPackageScope— tells the system that a given symbol should be exposed at the package levelPackageImport— tells the system to import a given context and expose it within that file
Any symbol not tagged with PackageExport or PackageScope gets a context that is private to that file.
A sample paclet structure might look like:
MyPaclet
+ PacletInfo.m
Kernel
+ init.m
MyPaclet.m
Component1.m
Component2.m
...
Each component would look like:
Package["MyPaclet`"]
PackageImport["Dependency1`"]
PackageImport["Dependency2`"]
...
PackageExport["Symbol1"]
PackageExport["Symbol2"]
...
PackageScope["PkgSym1"]
PackageScope["PkgSym2"]
...
sym1[]:=...
Symbol1[]:=...
...
Note a few interesting features:
- There is no
BeginorBeginPackageor anything like that - The tokens must be declared standalone, without even a
;at the end - We don't explicitly tell the system to load all the components—it does this automatically
This allows for much simpler package construction and is a good clean strategy for developing packages
Package` Context
Even better, there are tools at hand for analyzing/working with these packages. These are the functions in the "Package`" context. There'll be a little reference guide on them, but for now here's a quick rundown of how they work:
PackageInformation— extracts info for new style packagesLoadPackage— loads a new style packageDeclareLoad— sets up autoloading for a packageActivateLoad— set byDeclareLoadto delegate toLoadPackageCreatePackageCache— generates a .mx redux of a package for optimized loading