www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Private classes not really private?

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

According to the D spec:

"Private means that only members of the enclosing class can access the =
member, or members and functions in the same module as the enclosing =
class."

However:

[mod.d]
--------------
module mod;
private int x;

private class A
{}

[main.d]
--------------
import mod;

void main()
{
    writefln(x);    // Illegal, as it should be
    A a=3Dnew A;      // Shouldn't this be illegal?
}

Private variables in modules are inaccessible, but private classes are =
public..?
Jan 09 2005
parent reply John Reimer <brk_6502 yahoo.com> writes:
Jarrett Billingsley wrote:
 According to the D spec:
  
 "Private means that only members of the enclosing class can access the 
 member, or members and functions in the same module as the enclosing class."
  
 However:
  
 [mod.d]
 --------------
 module mod;
 private int x;
  
 private class A
 {}
  
 [main.d]
 --------------
 import mod;
  
 void main()
 {
     writefln(x);    // Illegal, as it should be
     A a=new A;      // Shouldn't this be illegal?
 }
  
 Private variables in modules are inaccessible, but private classes are 
 public..?

I'm not sure what the exact answer to this question is, but you will notice that there is a difference between "private int x;" which "instantiates" an actual variable and "private class A" which merely declares the details of a class; the instantiation of the class does not occur until the main function. Therefore, there's a distinct difference between the two declarations. With that understanding, I don't think that the "private" in the class A declaration should be expected to do the same thing. In fact, I'm not sure if the "private" keyword does anything in this instance. Perhaps the "package" attribute is useful in this circumstance. Later, John R.
Jan 09 2005
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 I'm not sure what the exact answer to this question is, but you will
 notice that there is a difference between "private int x;" which
 "instantiates" an actual variable and "private class A" which merely
 declares the details of a class; the instantiation of the class does not
 occur until the main function.  Therefore, there's a distinct difference
 between the two declarations.  With that understanding, I don't think
 that the "private" in the class A declaration should be expected to do
 the same thing.  In fact, I'm not sure if the "private" keyword does
 anything in this instance.  Perhaps the "package" attribute is useful in
 this circumstance.

Perhaps. However, wouldn't it make more sense (and be more useful) if we could make classes private to the module?
Jan 10 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
On Mon, 10 Jan 2005 18:54:07 -0500, Jarrett Billingsley  
<kb3ctd2 yahoo.com> wrote:

 I'm not sure what the exact answer to this question is, but you will
 notice that there is a difference between "private int x;" which
 "instantiates" an actual variable and "private class A" which merely
 declares the details of a class; the instantiation of the class does not
 occur until the main function.  Therefore, there's a distinct difference
 between the two declarations.  With that understanding, I don't think
 that the "private" in the class A declaration should be expected to do
 the same thing.  In fact, I'm not sure if the "private" keyword does
 anything in this instance.  Perhaps the "package" attribute is useful in
 this circumstance.

Perhaps. However, wouldn't it make more sense (and be more useful) if we could make classes private to the module?

Try this: --[mod.d]-- module mod; class A { private this() { } } --[main.d]-- import mod; void main() { A a = new A(); } Regan
Jan 11 2005