tupleof seems to break encapsulation
Jacob Carlborg
doob at me.com
Fri Sep 4 17:36:00 UTC 2020
On 2020-09-04 12:16, 60rntogo wrote:
> Consider the following code.
>
> foo.d
> ---
> module foo;
>
> struct Foo
> {
> private int i;
> }
> ---
>
> main.d
> ---
> void main()
> {
> import std.stdio;
> import foo;
>
> auto x = Foo();
> writeln(x);
> // ++x.i;
> ++x.tupleof[0];
> writeln(x);
> }
> ---
>
> As expected, the commented line does not compile. If I uncomment it, I
> get the error "no property i for type foo.Foo". However, the rest of the
> code compiles just fine and outputs:
> ---
> Foo(0)
> Foo(1)
> ---
>
> This appears to defeat the purpose of declaring i private. What am I
> missing?
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.
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list