C++ Logo

std-proposals

Advanced search

Re: Function proposal: generic value_or

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Mon, 5 Jul 2021 23:53:11 +0200
pon., 5 lip 2021 o 23:28 Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> On Mon, Jul 5, 2021 at 4:34 PM Roberto Romani via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> If the word “value” in the name of the function may create confusion then the function may be called “coalesce”.
>>
>>
>>
>> The goal is to have a function that makes cleaner and less error prone things like v = p1 ? *p1 : p2 ? *p2 : defaultValue;
>
>
> So basically, you claim that
> v = p1 ? *p1 : p2 ? *p2 : defaultValue;
> is too confusing for the reader to understand at a glance, and you think the reader would prefer to see

Where are clams of "confusing" in that quote (aside of name `value`)?
Roberto said that it makes code cleaner and this is true as you do not
repeat the same values.
and even could reduce complexity further:
```
auto p1 = foo();
auto p2 = bar();
return p1 ? *p1 : p2 ? *p1 : defualtValue;
```
vs
```
return std::coalesce(foo(), bar(), defaultValue); //I would prefer
default on last place but it could be hard to define
```

> v = std::coalesce(defaultValue, p1, p2);
> .
> And you also claim that it is not useful to provide any special library support for
> v = p1 ? p1[42] : defaultValue;
> nor for
> v = p1 ? *p1 : computeDefaultValue();
> .

Yes this is true, `p1[42]` could be too specific but
`computeDefaultValue` be probably useful
I could push even further to allow to every argument will be callable, like:

```
return std::coalesce_func(foo, bar, computeDefaultValue);
```

it will invoice each argument and check the result of this sequence.
In current code this probably would look like:
```
if (auto p1 = foo(); p1) return *p1;
if (auto p2 = bar(); p2) return *p2;
return computeDefaultValue();
```


>
> I don't buy either of these claims. Therefore, I recommend not pursuing this idea any further.
> However, if you do pursue it, you should start by finding about five places in existing open-source codebases where this library function would be applicable, and then showing how you'd rewrite those snippets to improve their... clarity, right? You're going for clarity here? So, show the "before and after." In the WG21 biz, this is known as a "Tony Table."
>
> –Arthur
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2021-07-05 16:53:24