Thursday, 31 August 2017

Implicitly imported units in a package.

Problem

Say, you write a package called MyControls.bpl. The units you put in it need a lot of units from TeeChart:

Package MyControls:

package MyControls;

...

contains
  MyUnit1, MyUnit2;

end.

Now, in MyUnit1 and myUnit2, you use the units TeeThis, TeeThat and TeeOther:

unit MyUnit1;

uses
  TeeThis, TeeThat;
unit MyUnit2;

uses
  TeeThat, TeeOther;

Then these three TeeXXX units will implicitly be imported (linked) into your package. So now your package contains MyUnit1, MyUnit2, TeeThis, TeeThat and TeeOther, although your contains section only explicitly mentions the first two, the ones you wrote.

Then, if a customer of yours installs your package, and then tries to install another package that needs these units too, the IDE will tell them to reference your package, because these units are installed already, and they can't be in any other package. That will happen even if your package knows nothing about the other, and the other knows nothing about yours. Because both need the same units.

Solution

Instead of implicitly using these units, reference the package that contains them:

package myControls;

...

contains
  MyUnit1, MyUnit2;

requires
  TeeChart;

end.

Now only your units will be in your package, and the units it needs will be used (referenced) from that other package. This way your package will (probably) be a lot smaller and other packages can use the TeeChart units in the same way.

Do not forget

Of course, now you will have to make sure that the user also has the TeeChart package. If you are allowed to distribute it (I guess so, but I am not a lawyer, so read the license), then you can do that and install it if the user doesn't have it installed yet.

Conclusion

Never, ever, ignore the message that certain units are implicitly imported into your package. Always make sure that this doesn't happen. Instead, reference a package that already contains them. Your packages will be smaller, and there will be no naming conflicts.