www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - struct visibility attributes

reply eugene <egordeev18 gmail.com> writes:
Hello, everyone,
i looked at 
https://dlang.org/spec/attribute.html#visibility_attributes but 
it says nothing about visibility attributes in structs.
The question is: why is the code compiled by ldc and working:

import std.stdio:writeln;
void main()
{
     test_struct testStruct = test_struct();
     testStruct.add_to_x; // add_to_x is private
     testStruct.add_to_x;
     writeln("x=", testStruct.x); // x is private
}
struct test_struct
{
     private:
         int x;
         void add_to_x()
         {
             x++;
         }
	
     public:
         int get_x()
         {
             return x;
         }
}
Aug 28 2016
next sibling parent eugene <egordeev18 gmail.com> writes:
LDC: ldc2-0.17.1-linux-x86_64
Aug 28 2016
prev sibling next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
Because private is to module?
Aug 28 2016
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Sunday, 28 August 2016 at 09:32:11 UTC, eugene wrote:
 Hello, everyone,
 i looked at 
 https://dlang.org/spec/attribute.html#visibility_attributes but 
 it says nothing about visibility attributes in structs.
inside a module you can even access to the private fields, it's clearly written in the specs:
 Symbols with private visibility can only be accessed from 
 within the same module.
Some languages have a "strict private" protection level to ensure a proper usage of the fields inside a same module, but not D.
Aug 28 2016