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

james.p.leblanc james.p.leblanc at gmail.com
Sun Aug 8 10:37:49 UTC 2021


On Sunday, 8 August 2021 at 10:19:46 UTC, jfondren wrote:
> 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.

Hej JFondren,

Wow, thanks for the quick response.  I had read that about the 
modules ...
but as my test example had failed, I thought that I had 
misunderstood
the larger picture.

Based on you kind reply, I went back over my example and found 
that I
had been deceiving myself.

With a quick fix-up edit, it indeed is working as your 
explanation.

Now, I proceed onto the trickier part of my endeavor ...

Thanks again, and Best Regards,
James

(Sorry for the noise...)



More information about the Digitalmars-d-learn mailing list