Non-transitive immutable? Read only struct.

Simen Kjærås simen.kjaras at gmail.com
Fri Dec 6 09:11:11 UTC 2019


On Thursday, 5 December 2019 at 17:01:07 UTC, Ola Fosheim Grøstad 
wrote:

> 1. Protect fields in structs/classes after initialization. 
> Reduces bugs. Documents intent.

immutable fields can be initialized in constructors. Getters 
allow you to define an interface that doesn't allow modification 
by outside forces while still allowing the class/struct to modify 
the fields if it feels like it, allowing both non-reassignable 
fields and lazy initialization.


> 2. Turn received values from functions into something that 
> cannot be modified reliably irrespective of what the protection 
> the called function has set for it. Reduces bugs.

Const does this. If you know it's immutable call assumeUnique on 
it. If you want to limit modification in some ways while allowing 
it in other ways, that sounds like a library thing, not something 
the language should do for you.


> 3. To have sensible immutable tuples that can point to 
> non-immutable things. In my view, a must have.

Getters can be used for this.


> 4. To have structures in ROM that can point to memory mapped 
> registers or RAM. Not a must have, but sensible.

Getters can do this. You'll probably have to dip into some 
un- at safe code, but this seems like an exotic enough use case that 
we can live with that.

--
   Simen


More information about the Digitalmars-d mailing list