tupleof seems to break encapsulation

60rntogo 60rntogo at gmail.com
Sat Sep 5 05:14:49 UTC 2020

On Friday, 4 September 2020 at 17:36:00 UTC, Jacob Carlborg wrote:
> It's useful for serialization and, as you can see in your 
> example, for debugging as well. `writeln` will print the values 
> of the fields in a struct, even for private fields.

I wouldn't dispute that it is useful, but that's besides the 
point. If I declare something private, it's usually because I 
want to preserve certain invariants and I want the compiler to 
provide a guarantee that I don't accidentally violate them. As it 
stands, the compiler cannot guarantee that if I use tupleof.

I don't really have an issue with read-only access to private 
fields (but arguments could be made against it) and then 
serialization would still be possible. However, if my struct is 
supposed to maintain invariants, then any attempt at 
deserialization that naively reads from a tuple without 
establishing these invariants should fail to compile.

More information about the Digitalmars-d-learn mailing list