C++ Logo

std-proposals

Advanced search

Re: Initial Idea of Indirect If Statement

From: Justin Bassett <jbassett271_at_[hidden]>
Date: Sat, 26 Oct 2019 16:24:25 -0700
This reminds me of pattern matching. I'm not sure if the pattern matching
proposal allows this, though. Rust would allow something like:

if let Some(x) = some_optional {
  ...
}

We propose a new variant of the if statement that fuses a condition
> declaration with an indirection in the true branch. We call it an
> 'indirect if statement'.
>

I find it a bit odd that this only works for unary `operator*`. I want this
to be more general, but I don't know if there's a good alternative without
going for full pattern matching.

An indirect if statement of the form:
>
> if (DECL : EXPR) X else Y;
>
> is equivalent to:
>
> {
> auto&& __c = EXPR;
> if (__c) {
> DECL = *__c;
> X;
> } else {
> Y; // noteworthy: DECL is not in scope here
> }
> }
>

It would sometimes be preferable to forward __c into DECL, but sometimes
not. E.g. if EXPR returned an optional<vector<...>> and DECL wanted to be a
value and not a reference. I don't know what the right choice is.

I do know that I'd use this if it was in the language.

--Justin

Received on 2019-10-26 18:26:54