write once type?

Steven Schveighoffer schveiguy at gmail.com
Tue Apr 20 20:04:17 UTC 2021


On 4/20/21 3:56 PM, Steven Schveighoffer wrote:
> I have had the need in some cases to *maybe* set a const value inside a 
> loop. One can sometimes abstract this into a lambda function, but 
> sometimes this is not possible (e.g. if the loop is static). Not only 
> that, but I may also want to keep processing the loop and do something 
> different if the value has already been set instead of returning 
> immediately, which necessitates a second loop.
> 
> My use case is I have a property that is const inside a set of types, 
> and I want to verify that they all have the same value, and extract what 
> that value is.
> 
> I'm wondering if anyone has a "Write once" type, that is, a type that 
> allows you to write it exactly once, and is treated like initialization 
> on first setting (i.e. allows writing to previously unused const data).
> 
> This type has to decide at runtime whether it has been set. So it would 
> kind of be like Nullable!T, but Nullable doesn't allow setting when the 
> T is const. Optional!T doesn't work either.

I just realized, this is Rebindable, or tail-const. (I don't need the 
head to be truly const, I just need to be able to copy into a 
const-referring thing). I currently am using a pointer, which is 
working, but I prefer not to use it, and it is not general enough.

-Steve


More information about the Digitalmars-d-learn mailing list