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

james.p.leblanc james.p.leblanc at gmail.com
Sun Aug 8 10:47:39 UTC 2021


On Sunday, 8 August 2021 at 10:40:51 UTC, Ali Çehreli wrote:

>
> I understand your question differently from jfondren. You may 
> be looking for a 'const' (or 'immutable') member:
>
> struct S {
>   const int i;
>
>   this(int i) {
>     // This will work because "first assignment is 
> initialization"
>     this.i = i;
>   }
> }
>
> void main() {
>   auto s = S(42);
>
>   // This won't work
>   s.i = 43;
>
>   // This won't work either
>   s = S(44);
> }
>
> Ali

Hello Again Ali,

Excellent!  I had tried (an erroneous) variant of this idea 
earlier ... but
also failed with my attempt.

I am appreciating very much the example you have provided.  I 
will try
this approach as well for the problem I am working on.  (Some 
details on
my path forward remain unclear ...)

Best Regards,
James



More information about the Digitalmars-d-learn mailing list