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

Open
opened 2026-01-31 07:14:19 +00:00 by claunia · 1 comment
Owner

Originally created by @zadjii-msft on GitHub (Sep 7, 2023).

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

Originally created by @zadjii-msft on GitHub (Sep 7, 2023). From a teams chat ### example 1 ```c++ int Foo() {...} optional<int> whatever { /*...*/ }; ... int a = til::coalese(whatever, Foo) ``` `Foo` only gets called if `whatever` is empty. ### example 2 ```c++ 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>` ```c++ 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
claunia added the Help WantedIssue-TaskProduct-MetaArea-CodeHealth labels 2026-01-31 07:14:20 +00:00
Author
Owner

@lhecker commented on GitHub (Sep 7, 2023):

Parts of the coalesce code will become obsolete with C++23 and the 3 monadic functions listed here: https://en.cppreference.com/w/cpp/utility/optional
If we want to do this, I think we should take inspiration from those for the design.

@lhecker commented on GitHub (Sep 7, 2023): Parts of the coalesce code will become obsolete with C++23 and the 3 monadic functions listed here: https://en.cppreference.com/w/cpp/utility/optional If we want to do this, I think we should take inspiration from those for the design.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20451