private module stuff

Sean Cavanaugh WorksOnMyMachine at gmail.com
Sun May 8 05:48:49 PDT 2011


On 5/8/2011 4:05 AM, Jonathan M Davis wrote:
>> Sean Cavanaugh:
>>> 	So I was learning how to make a module of mine very strict with private
>>>
>>> parts, and was surprised I could only do this with global variables and
>>> functions.   Enums, structs, and classes are fully visible outside the
>>> module regardless of being wrapped in a private{} or prefixed with
>>> private.  Am I expecting too much here?
>>
>> You are expecting the right thing. If you are right, then it's a bug that
>> eventually needs to be fixed. Take a look in Bugzilla, there are several
>> already reported import/module-related bugs.
>
> They're private _access_ but still visible. I believe that that is the correct
> behavior and not a bug at all. I believe that it's necessary for stuff like
> where various functions in std.algorithm return auto and return a private
> struct which you cannot construct yourself. Code which uses that struct needs
> to know about it so that it can use it properly, but since it's private, it
> can't declare it directly. It works because the types are appropriately
> generic (ranges in this case). Regardless, I believe that the current behavior
> with private is intended.
>
> - Jonathan M Davis


The more I play with private/protected/package the more confused I am by it.

For the most part the rules are:
	Functions and variables have protection
	Types (enum, struct, class) do not
	this and ~this are special and are not considered functions, and are 
always public
	struct and class members always default to public



If you search phobos you will find occurences of 'private struct' and 
'private class', so even the people writing libraries are expecting 
something to be happening that isn't.  For example:


//in std.parallelism:
private struct AbstractTask {
     mixin BaseMixin!(TaskStatus.notStarted);

     void job() {
         runTask(&this);
     }
}


//and in std.demangle:
private class MangleException : Exception
{
     this()
     {
         super("MangleException");
     }
}


and in my code I can compile the following without compile-time errors:


import std.parallelism;
import std.demangle;

int main()
{
	MangleException bar = new MangleException();

	AbstractTask foo;
	foo.job();

	return 0;
}




With the language the way it is now, it is nonsensical to have the 
attributes public/protected/package/private/export precede the keyword 
struct, class, or enum.


More information about the Digitalmars-d-learn mailing list