Search code examples
d

In D, what's the difference between a private import and a normal import?


In the D programming language, what is the difference between

private import tango.io.File;

and

import tango.io.File;

?


Solution

  • There was a time when imports were public by default; that is, when you imported another module, its contents would not only be visible from within your module but also from any module that imported your module.

    Eventually, it was changed so that they were private by default.

    However, there's a few reasons to manually specify private:

    1. Imports can be made public if they're in a public context. For example:

      public:
      
      // Lots of stuff
      
      import blah; // oh no, everyone can see my imports!
      
    2. DMD teems with import-related bugs. For example, selective imports are public by default in spite of supposedly being private. This can cause all sorts of horrible nightmare scenarios where symbols (erroneously) imported publically in one module cause symbols in a completely different module to simply vanish and break your program.

    In other words, they're probably private in Tango because the devs have worked with DMD for too long to trust it to get this stuff right.