Why constructs can not be private?

Jonathan M Davis jmdavisProg at gmx.com
Tue Mar 6 20:51:54 PST 2012


On Tuesday, March 06, 2012 22:24:55 Caligo wrote:
> module A;
> 
> private struct A { }
> private A a;
> private mixin template magic() { }
> private void foo() { }
> 
> //---------------------
> 
> module B;
> 
> import A;
> 
> void main() {
>   A b;  // #1 works

Doesn't construct anything. It just uses A's init property. It's perfectly 
legal to copy structs even if the struct is private. For instance, many of 
std.algorithm's functions return nested types, so it's impossible for anything 
else to construct them, but you can still copy the result. That's not exactly 
the same as private (since they're nested, not private), but I believe that 
the situation is essentially the same. It allows you to pass around and copy 
stuff that you can't legally create but a function which you _can_ call 
returned to you.

>   b = a; // #2 ERROR

a is private. So, this module can't access it.

>   foo();  // #3 ERROR

foo is private. So, this module can't access it.

> }
> 
> struct B{
> 
>  mixin magic;  // #4 works

Access modifiers are currently ignored for templates. It's definitely a bug:

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

> }
> 
> 
> What's the point of declaring struct A private if it's not going to
> behave as such?  is this another bug in DMD?  I want template mixin
> magic to not be accessible outside module A.  Would that be possible?

It will be once bug# 2775 has been fixed, but until then, no.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list