www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - package & protected?

reply Manu <turkeyman gmail.com> writes:
So, a great many class members tend to be `protected`... that's just how
classes are.
We also avoid `friend` in D with `package`, which I find to often be quite
useful.

The trouble is... in a high number of cases where I want to make something
`package`, it ALSO should be `protected`, but these 2 are mutually
exclusive.

This is a problem. We need a way to express both... what's the story here?
Oct 21
next sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
On Monday, 21 October 2024 at 07:49:18 UTC, Manu wrote:
 So, a great many class members tend to be `protected`... that's 
 just how
 classes are.
 We also avoid `friend` in D with `package`, which I find to 
 often be quite
 useful.

 The trouble is... in a high number of cases where I want to 
 make something `package`, it ALSO should be `protected`, but 
 these 2 are mutually exclusive.
Sounds like a design issue on your end If it is hidden, it doesn't need to be abstracted You don't need OOP
Oct 21
prev sibling next sibling parent Max Samukha <maxsamukha gmail.com> writes:
On Monday, 21 October 2024 at 07:49:18 UTC, Manu wrote:

 This is a problem. We need a way to express both... what's the 
 story here?
I guess the answer would be: it's your package, so you control it and don't need to protect it from yourself. At least, that was part of the argument against class-private (as I understood it): the module is the API boundary, why would you protect anything inside? Ironically, packages are not sealed, so 'package' doesn't really protect anything at all.
Oct 21
prev sibling next sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Monday, 21 October 2024 at 07:49:18 UTC, Manu wrote:
 
 This is a problem. We need a way to express both... what's the 
 story here?
Yes. There is no such thing as hiding the facilities inside a module from each other in D. It may seem strange when compared to languages ​​like C++, but it actually reveals the practical side of D: Why shouldn't a programmer be able to access the facilities inside a module while working in it? Who are we protecting from whom? SDB 79
Oct 21
prev sibling parent reply Atila Neves <atila.neves gmail.com> writes:
On Monday, 21 October 2024 at 07:49:18 UTC, Manu wrote:
 So, a great many class members tend to be `protected`... that's 
 just how
 classes are.
 We also avoid `friend` in D with `package`, which I find to 
 often be quite
 useful.

 The trouble is... in a high number of cases where I want to 
 make something `package`, it ALSO should be `protected`, but 
 these 2 are mutually exclusive.

 This is a problem. We need a way to express both... what's the 
 story here?
My guess is the story is that nobody needed/wanted that before. I'm struggling to understand what the use case would be; I would love to know what design led to this situation.
Oct 22
parent reply Kagamin <spam here.lot> writes:

have a protected member, but also access it across a package. A 
workaround is to have an accessor:
```
class A
{
	protected int b;
	final package int b2(){ return b; }
}
```
Oct 23
parent Kagamin <spam here.lot> writes:
Another option is an accessor class:
```
class A
{
	protected int b;
	package class Accessor
	{
		int b(){ return this.outer.b; }
	}
}

void f1()
{
	A a=new A;
	a.b=2;
	auto aa=a.new Accessor;
	assert(aa.b==2);
}
```
Oct 23