write once type?

Steven Schveighoffer schveiguy at gmail.com
Tue Apr 20 19:56:33 UTC 2021


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.

-Steve


More information about the Digitalmars-d-learn mailing list