[Issue 1441] New: Allow 'private' to restrict class visibility outside module or outer class

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Aug 26 11:22:58 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1441

           Summary: Allow 'private' to restrict class visibility outside
                    module or outer class
           Product: D
           Version: 2.003
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: nathaniel.reed at gmail.com


Currently, there is no way to restrict a class from being visible outside the
module where it is defined, or for inner classes, outside its outer class.

This isn't a bug, more of a feature request, but it would be nice to be able to
use 'private' on a class to hide its visibility:

module A;
private class Foo { ... }

module B;
import A;
Foo f = new Foo; // error, Foo is private

also:

class Foo {
    private static class Bar { ... }
    ...
}

Foo.Bar b = new Foo.Bar; // error, Bar is private

I know a private constructor can be used to prevent a class's *instantiation*
from another module, but this seems like a hackish workaround to me.

Modules should be able to prevent their classes from being visible in another
module for at least two reasons:
1. Encapsulation: for the same reasons that private methods of a class aren't
visible when its containing module is imported, classes that are part of the
implementation of the module aren't visible when the module is imported.
2. Namespace pollution: modules can prevent names that are part of their
implementation from unneccessarily entering the namespace of an importing
module.
The same applies to inner classes.

Thanks,
Nathan Reed


-- 



More information about the Digitalmars-d-bugs mailing list