Suppose we have a vehicle, which gets its position from an IMU. The IMU package consists of several private components, which it orchestrates to calculate the vehicle's state in space:
with IMU; use IMU;
Package Vehicle is
...
end vehicle;
Package IMU is
type Attitude is record
Lat, Lon: Angle;
Pitch, Roll, Yaw: Angle;
GroundSpeed: Speed;
...
end record
function Get_Position return Attitude is ...
Package GPS is
...
end GPS;
Package Accelerometer is
...
end Accelerometer;
Package Gyro is
...
end Gyro;
end IMU;
The internals of GPS, Accelerometer and Gyro only make sense in the context of the IMU; they must be completely hidden from Vehicle.
Declaring the IMU and all of its sub-components in a single source file will be difficult to read and maintain; I'd like to have each sub-component in it's own source file. If I code them all at the IMU level, the vehicle will be able to access the GPS, which is wrong.
What is the best practice for structuring nested packages?
There are several ways to structure your code but instead of using nested packages, I would use a private child for each "component".
This way, each component would be in its own unit and be visible from its parent and its siblings implementations.
Your code would then be
Package IMU is
type Attitude is record
Lat, Lon: Angle;
Pitch, Roll, Yaw: Angle;
GroundSpeed: Speed;
...
end record
function Get_Position return Attitude is ...
end IMU;
private package IMU.GPS is
...
end IMU.GPS;
private package IMU.Accelerometer is
...
end IMU.Accelerometer;
private package IMU.Gyro is
...
end IMU.Gyro;
See the wikibook for other examples