www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Class declaration

reply negerns <negerns2000 gmail.com> writes:
I have been testing/configuring a UML tool to generate D classes/files 
when I came across D's class attributes scope and final. I wondered if D 
classes have other attributes like public, private, protected. So I 
tried it and tested some D source codes. Here is one that I tried:

private protected public class A {
     public this() { }
}

void main() {
     auto a = new A();
}

This code was successfully compiled using dmd 1.021 without errors or 
warnings, much to my surprise. Could anyone please tell me why is this so?

-- 
negerns
Oct 05 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
negerns wrote:
 I have been testing/configuring a UML tool to generate D classes/files 
 when I came across D's class attributes scope and final. I wondered if D 
 classes have other attributes like public, private, protected. So I 
 tried it and tested some D source codes. Here is one that I tried:
 
 private protected public class A {
     public this() { }
 }
 
 void main() {
     auto a = new A();
 }
 
 This code was successfully compiled using dmd 1.021 without errors or 
 warnings, much to my surprise. Could anyone please tell me why is this so?

You're certainly not the first one to ask. :-) First off protection attributes don't apply within a module. Everything in one file has access to everything else. D doesn't have C++'s "friend" so that's sort of D's substitute. Second, the compiler is overly permissive about mixed attributes. I think there are bugs on file for it. But fixing it is low priority since the amount of harm it causes is small. --bb
Oct 05 2007
parent reply "Simen Haugen" <simen norstat.no> writes:
module test;
import std.stdio;

private protected public public private public private protected public 
protected public private protected private public public private protected 
protected private class T {
 this(char[] msg) {
  writefln(msg);
 }
}

void main() {
 T t = new T("This works too :)");
}


"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:fe4tra$2bgd$1 digitalmars.com...
 negerns wrote:
 I have been testing/configuring a UML tool to generate D classes/files 
 when I came across D's class attributes scope and final. I wondered if D 
 classes have other attributes like public, private, protected. So I tried 
 it and tested some D source codes. Here is one that I tried:

 private protected public class A {
     public this() { }
 }

 void main() {
     auto a = new A();
 }

 This code was successfully compiled using dmd 1.021 without errors or 
 warnings, much to my surprise. Could anyone please tell me why is this 
 so?

You're certainly not the first one to ask. :-) First off protection attributes don't apply within a module. Everything in one file has access to everything else. D doesn't have C++'s "friend" so that's sort of D's substitute. Second, the compiler is overly permissive about mixed attributes. I think there are bugs on file for it. But fixing it is low priority since the amount of harm it causes is small. --bb

Oct 05 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Simen Haugen wrote:
 module test;
 import std.stdio;
 
 private protected public public private public private protected public 
 protected public private protected private public public private protected 
 protected private class T {
  this(char[] msg) {
   writefln(msg);
  }
 }
 
 void main() {
  T t = new T("This works too :)");
 }

In an ideal world, compiling the above would produce: Error test.d:4: programmer is being a smartass. :3 -- Daniel
Oct 05 2007