tags:

views:

101

answers:

2

In the D programming language, what is the difference between

private import tango.io.File;

and

import tango.io.File;

?

+4  A: 

In D 2.0, private import is synonymous with import, as opposed to public import. By default, imports are private. See the Modules documentation

EDIT: By default, imports are private D 1.0, too.

stephan
+4  A: 

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.

DK
+1 ah, right, bugs...
stephan
re pt 2 are you talking about D2 or D1?
Dave Berk
I don't use D2. That said, I'd be somewhat surprised if D2 doesn't have the same problems; dmd 1.x and dmd 2.x are built from the same source code, just with different pre-processor defines. Imports haven't really changed between 1.x and 2.x, as far as I'm aware.
DK