Thread overview
Problems with protection attributes
Oct 27, 2005
BCS
Oct 27, 2005
Derek Parnell
Oct 27, 2005
Ivan Senji
October 27, 2005
I know this had been brought up before, but it has never gotten a good assure as far as I am concerned.

What exactly is a package????

The following code compiles fine (XP DMD.135) but If I'm reading the docs right, it should generate an error when module "sub1.mod1" uses the package class "sub2.mod2.Class2"

Am I missing something or is this not working right?


module root;
import sub1.mod1;
import sub2.mod2;

int main()
{
Class1 c1 = new Class1;
Class2 c2 = new Class2;

c1.use();
c2.use();
}

//////////

module sub1.mod1;

import sub2.mod2;

public class Class1
{
Class2 use(){ return new Class2;}
}

//////////

module sub2.mod2;

import sub1.mod1;

package class Class2
{
Class1 use(){ return new Class1;}
}


October 27, 2005
On Thu, 27 Oct 2005 04:14:25 +0000 (UTC), BCS wrote:

> I know this had been brought up before, but it has never gotten a good assure as far as I am concerned.
> 
> What exactly is a package????
> 
> The following code compiles fine (XP DMD.135) but If I'm reading the docs right, it should generate an error when module "sub1.mod1" uses the package class "sub2.mod2.Class2"
> 
> Am I missing something or is this not working right?
> 
> module root;
> import sub1.mod1;
> import sub2.mod2;
> 
> int main()
> {
> Class1 c1 = new Class1;
> Class2 c2 = new Class2;
> 
> c1.use();
> c2.use();
> }
> 
> //////////
> 
> module sub1.mod1;
> 
> import sub2.mod2;
> 
> public class Class1
> {
> Class2 use(){ return new Class2;}
> }
> 
> //////////
> 
> module sub2.mod2;
> 
> import sub1.mod1;
> 
> package class Class2
> {
> Class1 use(){ return new Class1;}
> }

The 'package' attribute is designed to work on *members* of a class and not the class itself.  The compile will fail if you try this ...

module sub2.mod2;
import sub1.mod1;
class Class2
{
 package {
   Class1 use(){ return new Class1;}
 }
}

Though another way to foil misuse of the class might be ...

class Class2
{
   package this() {}
   Class1 use(){ return new Class1;}
}


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
27/10/2005 3:20:41 PM
October 27, 2005
Derek Parnell wrote:
> The 'package' attribute is designed to work on *members* of a class and not
> the class itself.  The compile will fail if you try this ...
> 

HM, i am just wondering why a class should not be allowed to be package class?
October 27, 2005
"Ivan Senji" <ivan.senji_REMOVE_@_THIS__gmail.com> wrote in message news:djq4at$1dls$1@digitaldaemon.com...
> HM, i am just wondering why a class should not be allowed to be package class?

I don't see why it shouldn't be able to be.  Classes also can't have "private" applied to them, which is an annoying shortcoming IMO.

There have always been odd problems with the protection attributes in D. Thankfully those problems with structs have been fixed, but there's still another really annoying one - an outer class cannot access an inner class's private / protected members, even though they are in the same file (obviously).  That said, I've also never been able to get "package" to work, or at least not in the way that I think it should.