struct's fields are all public? no access control?

mw mingwu at gmail.com
Fri Apr 2 15:35:03 UTC 2021


https://run.dlang.io/is/xent7k

---
import std;

struct Lady {
     private int age;
     int height;
     float ageXHeight;
}

void main()
{
     Lady p;
     p.age = 20;      // why can access private field here?
     writeln(p.age);  // why can access private field here?
}
---

Is this a compiler bug, or by language design?

I'd argue it should honor programmer's intention: to protect some 
data fields from outside access, they are intended to be kept as 
internal state of the struct.



More information about the Digitalmars-d mailing list