write once type?

ichneumwn idonotenjoyemail at idonotenjoyemail.org
Tue Apr 20 20:22:25 UTC 2021


On Tuesday, 20 April 2021 at 19:56:33 UTC, 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.
>
> -Steve

Not quite your use case, I think, but std.typecons.Rebindable at 
least allows you to bind a new const(object). Not write once 
though.

I think I found that after a hint of its existence in
https://forum.dlang.org/thread/orpbvvjspibfpitnnpxd@forum.dlang.org




More information about the Digitalmars-d-learn mailing list