C++ Logo

std-proposals

Advanced search

Re: Initialisers in ternary operators

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Sun, 13 Sep 2020 12:25:39 +0200
niedz., 13 wrz 2020 o 05:29 Jorg Brown via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> On Fri, Sep 11, 2020 at 6:29 AM Richard Hodges via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> On Fri, 11 Sep 2020 at 12:39, Garrett May via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>>>
>>>> Hm .. who said,(in the context of C++ talk): "don't be clever unless you really have to"?
>>>
>>> I'm not really sure what that means?
>>>
>>> Either way, my personal preference would be in support of Richard Hodges's suggestion. I've found GCC's expression statements to be useful in the past, and I think standardising it would be a perfect solution for this. It avoids the weirdness of having an initializer only for ternary operators that Ville Voutilainen mentioned (due to the syntax of ternaries) whilst also providing a way for scope to yield a value.
>>
>>
>> All that remains now is for someone to write a paper, in which is laid out the motivating use case, a demonstration that existing language features are limiting productivity and an impact assessment.
>>
>>
>> There is already a compiler supporting the feature, so that's in the paper's favour.
>>
>> I suspect the biggest problems would be:
>> a) convincing everyone that the existing lambda syntax is not sufficient
>
>
> Regarding statement expressions, I dislike deviations from the standard in Google's code, so I tried to get rid of statement expressions when adopted c++11 and got lambdas. And indeed, lambdas solve much of the need.
>
> But there was one strong outlier, and it looks like this:
>
> std::optional<std::string> oldFindUsersCity(bool non_default) {
> std::optional<UserId> uid = UserId{};
> if (non_default) {
> uid = GetUserId();
> if (!uid) return nullopt;
> }
> std::optional<Location> uloc = uid->GetLocation();
> if (!uloc) return nullopt;
> return uloc->GetCityName();
> }
>
> Those if/return pairs make the code really ugly really fast, so people made macros:
>
> std::optional<string> FindUsersCity(bool non_default) {
> UserId uid;
> if (non_default) ASSIGN_OR_RETURN(uid, GetUserId());
> ASSIGN_OR_RETURN(Location uloc, uid.GetLocation());
> return uloc.GetCityName();
> }
>

I recall that at some point someone suggest something like:
```
operation<std::optional<string>> FindUsersCity(bool non_default) {
   UserId uid;
   if (non_default) uid = co_await break_if_null(GetUserId());
   Location uloc = co_await break_if_null(uid.GetLocation());
   co_return uloc.GetCityName();
}
```
of course this is a bit heavier and would need some tweaks to corutine
functionality.
Question is it worthly to mix these two concepts.

Received on 2020-09-13 05:25:52