How do I create a module-local immutable class object?

Steven Schveighoffer schveiguy at yahoo.com
Mon Sep 12 05:08:00 PDT 2011


On Sat, 10 Sep 2011 17:06:44 -0400, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> On 9/10/11, bearophile <bearophileHUGS at lycos.com> wrote:
>> Andrej Mitrovic Wrote:
>>
>>> Wait a minute, I've just realized private on a class definition has no
>>> effect. Why is that?
>>
>> Try to import that class from another module...
>
> It doesn't stop imports, that's what I'm saying. I can import and
> instantiate the class from another module even though it's a private
> class definition.

For a long time, protection attributes have no effect on a global scope.   
I personally think they should either work (only private/package/public)  
or be a compiler error.  To silently be accepted is really confusing.

However, on an inner scope, you can declare a private class/struct:

a.d:
class C
{
    private struct S
    {
    }
}


main.d:
import a;

void main()
{
    C.S s; // error
}

-Steve


More information about the Digitalmars-d-learn mailing list