Why is D unpopular
Paul Backus
snarwin at gmail.com
Mon Jun 13 13:51:40 UTC 2022
On Monday, 13 June 2022 at 08:44:59 UTC, bauss wrote:
> On Monday, 13 June 2022 at 08:07:05 UTC, Mike Parker wrote:
>>
>> But why should that compile? You're trying to manipulate `_c`
>> through an instance of `Bar`. It's not visible in any `Bar`,
>> ever, so why should it be visible here? It has to be gotten at
>> through the interface of `Foo`.
>
> Because I'm in the module of a, _c is a member if Foo, Foo is
> in a.
>
> Thus _c should be accessible within a regardless of whether
> it's public within the b module or not.
`_c` is accessible; you just have to use the syntax
`child.Foo._c` to access it. This is documented in the language
spec:
> Members of a base class can be accessed by prepending the name
> of the base class followed by a dot
https://dlang.org/spec/class.html#fields
This is necessary because D allows you to define fields with the
same name in a base class and its derived class; for example:
```
class Base
{
int x = 123;
}
class Derived : Base
{
int x = 456;
}
void main()
{
auto instance = new Derived;
assert(instance.x == 456);
assert(instance.Base.x == 123);
}
```
More information about the Digitalmars-d
mailing list