Does the 'package' protection attribute not work?

Jonathan M Davis jmdavisProg at gmx.com
Sun Aug 7 18:08:59 PDT 2011


On Monday 08 August 2011 00:19:56 Stijn Herreman wrote:
> On 7/08/2011 23:18, Jonathan M Davis wrote:
> > On Sunday 07 August 2011 18:58:53 Stijn Herreman wrote:
> >> module main;
> >> 
> >> import std.stdio;
> >> import my_module;
> >> 
> >> int main()
> >> {
> >> 
> >> 	my_method();
> >> 	return 0;
> >> 
> >> }
> >> 
> >> 
> >> module my_module;
> >> 
> >> import std.stdio;
> >> 
> >> package void my_method()
> >> {
> >> 
> >> 	 writeln("Hello D-World!");
> >> 
> >> }
> >> 
> >> 
> >> Error: function my_module.my_method is not accessible from main
> > 
> > Hmmm. My guess would be that either it's a bug or that from D's
> > perspective, neither of your modules are in a package. They have no
> > package in front of their names; they're at the base level of the
> > hierarchy. And that might mean that they don't have a package, so they
> > don't share a package. But I don't know.
> > 
> > Personally, I don't see much point in using the package specifier when
> > you're not actually using a package hierarchy (you're just making it so
> > that everything but stuff which actually uses a hierarchy can use the
> > function - it would be a really weird distinction to make). So, it
> > wouldn't entirely surprise me if this is completely by design. It might
> > be a bug though.
> > 
> > - Jonathan M Davis
> 
> The actual code does make use of a package hierarchy. At least, I'm
> under the impression it does: the files are in a subdirectory.
> Explicitly stating the package and module name in the files make the
> 'package' attribute work.
> 
> So either the attribute does not work when the package and module name
> aren't explicitly stated, or a directory does not equal a package.
>  From "The D Programming Language": "we refer to [...] a directory
> containing D source files as a package."

TDPL is correct with regards to how packages are _supposed_ to work. Whether 
they _actually_ work that way at the moment, I don't know. I've rarely had use 
for them, so I'm not sure that I've ever actually tried it. There's every 
possibility that package is currently broken. But what you've read in TDPL is 
definitely how packages are supposed to work.

- Jonathan M Davis


More information about the Digitalmars-d mailing list