How to create an overwriteable struct that is always const?

ag0aep6g anonymous at example.com
Sun Jun 2 09:18:05 UTC 2019


On 02.06.19 04:22, David Zhang wrote:
> On Saturday, 1 June 2019 at 13:00:50 UTC, ag0aep6g wrote:
> 
>> How is setting/replacing different from modifying?
> 
> e.g.:
> 
>      S s;
> 
>      this() { s = ...; }
> 
>      update(S s) { this.s = s; }
> 
>      mod(int i) { s.i = i; } // illegal
> 
> Kinda like how strings can be copied and assigned to, but not modified.

The `string` itself can be modified: You can change its length, and you 
can make it refer to other characters. That's modifying.

You can't modify the string's characters, because the string refers to 
them with an `immutable(char)*` pointer. That means the pointer itself 
is mutable (can be modified), but the data it points to is immutable 
(can't be modified).

You can do the same in your struct: `const(char)* pointer;`. Then you 
can modify the pointer but you can't modify the data it points to.


More information about the Digitalmars-d-learn mailing list