Skip to content

til::coalesce<T> should be able to take a function that returns T #15940

Open
@zadjii-msft

Description

@zadjii-msft

From a teams chat

example 1

int Foo() {...}
optional<int> whatever { /*...*/ };
...

int a = til::coalese(whatever, Foo)

Foo only gets called if whatever is empty.

example 2

class Foo{
  optional<int> _cached;
  int GetThing();
  int _generate();
}

int Foo::GetThing()
{
    _cached = coalesce(_cached, [&](){ return _generate(); });
    return _cached;
}
int Foo::_generate() { /*...*/ }

Look ma, now it's easy to implement lazy properties. So easy in fact...

example 3 - til::lazy_property<T>

class Foo {
  til::lazy_property<int> Thing { { this, &Foo::_generate } }; // same syntax as winrt event handlers
  int _generate();
}

// ...

Foo foo{};
int x = foo.Thing(); // Calls _generate
int y = foo.Thing(); // Doesn't
foo.Thing.invalidate(); 
int z = foo.Thing(); // calls generate again

(maybe we don't do this one. Seems like it's potentially footgun-y)


This is a truly one of the ideas I had while working on Profile::EvaluatedIcon in #15843

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-CodeHealthIssues related to code cleanliness, linting, rules, warnings, errors, static analysis, etc.Help WantedWe encourage anyone to jump in on these.Issue-TaskIt's a feature request, but it doesn't really need a major design.Product-MetaThe product is the management of the products.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions