microsoft / microsoft/terminal

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

Open
#15,940 4 comments 0 reactions 0 assignees View on GitHub
Area-CodeHealth Help Wanted Issue-Task Product-Meta
Dominant language
C++
Stars
105k
Forks
9.6k
Avg merge
3d 17h
Merged PRs (30d)
29

Description

From a teams chat

### example 1

```c++
int Foo() {...}
optional whatever { /*...*/ };
...

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

`Foo` only gets called if `whatever` is empty.

### example 2
```c++
class Foo{
optional _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`

```c++
class Foo {
til::lazy_property 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

Contributor guide

Open the contributing guide

Research direction

No file or test is named. Start by locating til::coalesce and its existing callers or coverage, then compare the requested behavior with examples 1 and 2; treat til::lazy_property as optional, since the issue questions whether to include it. Done means the agreed coalesce behavior is implemented and covered by relevant tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.