Proper way to protect (lock) a struct field after initialization ??

jfondren julian.fondren at gmail.com
Sun Aug 8 10:19:46 UTC 2021


On Sunday, 8 August 2021 at 10:11:37 UTC, james.p.leblanc wrote:
> Hello All.
>
> Is there a standard way to protect a field of a struct after
> the struct has been initialized?
>
> Is this possible with a struct?
>
> If not, I suppose a class (object) would be needed?  If so,
> are there any simple pointers to an example of this?
>
> Thanks in advance,
>
> James

`private` works for structs the same as it does for classes.
https://dlang.org/spec/attribute.html#visibility_attributes

Perhaps you tried it, realized you could still access it within 
the same module, and concluded that it didn't work? Consider note 
#2 at that link: "Symbols with private visibility can only be 
accessed from within the same module." Import the struct into 
another module and test the visibility there and you'll get the 
behavior you're looking for.


More information about the Digitalmars-d-learn mailing list