Lazily Initialized Variables that are Compatible with const/immutable
Vijay Nayar
madric at gmail.com
Thu Nov 17 12:26:00 UTC 2022
On Thursday, 17 November 2022 at 12:16:41 UTC, Siarhei Siamashka
wrote:
> On Thursday, 17 November 2022 at 10:57:55 UTC, Vijay Nayar
> wrote:
>> Is there another way to perform lazy initialization that is
>> compatible with const/immutable, or is this needed as a
>> language feature?
>
> Wouldn't precomputing this const/immutable data at compile time
> via CTFE be a better idea in general?
>
> Unless it's some sort of a sparse data structure with a huge
> memory footprint and the goal is to postpone memory allocation.
> But then you would probably also want to free memory as soon as
> possible after you are done using it and const/immutable would
> be a hindrance again.
CTFE would indeed have better performance for data that is known
at compile time, but often, the objects created which need lazy
evaluation, are specific to the instance and runtime data.
A few examples include:
* Initializing costly hardware connections/setup.
* Creating large objects used to organize or index data that
are only needed sometimes, e.g. when serializing for writing to
disk.
* Retrieving information from a network, e.g. a JWT or other
authorization information to be used for a session.
More information about the Digitalmars-d
mailing list