How can I do lazy variable initialization?

Jack jckj33 at gmail.com
Sat Jan 9 20:16:45 UTC 2021


In c# we can do something like this:


>  static Lazy<T> lazy =
>        new Lazy<T>
>            (() => heavyLoadOperation());
>  static T value { get { return lazy.Value; } }

and heavyLoadOperation() is only called when variable "value" is 
actually used. How can I do something like this in D?

I've tried something like this I still can't make it compile:

>T delegate() lazy lazy = { return heavyOperation(); }
>T X(lazy T delegate() dg) { return dg(); }
>T value = X;




More information about the Digitalmars-d-learn mailing list