On Sunday, 8 April 2018 at 13:00:02 UTC, bauss wrote:
I think it's better demonstrated like this, because to me the
behavior makes no sense.
Especially since you can just cast "Bar" to "Foo" and then
you're allowed to do it.
Since we're inside Foo then it shouldn't care whether "_baz" is
private or not.
I could understand if the function was located within Bar, but
it's not.
It's perfectly normal in other languages that supports classes
to access private members of parent's as long as you're within
the parent's encapsulation.
// a.d
module a;
class Foo
{
private:
bool _baz;
public:
void handleBar(T : Foo)(T[] foos)
{
foreach (child; foos)
{
child._baz = true; // Not ok.
(cast(Foo)child)._baz = true; // Ok.
}
}
}
// b.d
module b;
import a;
class Bar : Foo
{
}
// main.d
module main;
import b;
void main()
{
auto bars = [new Bar, new Bar];
auto bar = new Bar;
bar.handleBar(bars);
}
Actually, this behaves as i would expect.
`_baz` is a private member of Foo (to be precise: it belongs to
module `a`)
in handleBar(), you iterate `Bar[]` - which is in module `b`.
By casting it to Foo, you are accessing the wanted module (`a`)
again.