lazy variables

Paul Backus snarwin at gmail.com
Wed Oct 17 23:34:55 UTC 2018


On Wednesday, 17 October 2018 at 07:32:37 UTC, aliak wrote:
> lazy S x = () {
>     // do some heavy stuff
> }();
>
> if (condition) {
>   func(x.y); // heavy stuff evaluated here
> }

auto x = () {
     // do some heavy stuff
};

if (condition) {
     func(x().y); // heavy stuff evaluated here
}

If you want to make it a little prettier, you could define a 
couple helper functions:

T delegate() delay(lazy T expr)
{
     return () => expr;
}

T force(T delegate() thunk)
{
     return thunk();
}


More information about the Digitalmars-d-learn mailing list