Jump to page: 1 24  
Page
Thread overview
If Statement with Declaration
Jul 19, 2017
sontung
Jul 19, 2017
Adam D. Ruppe
Jul 19, 2017
Dukc
Jul 19, 2017
Nemanja Boric
Jul 19, 2017
Nemanja Boric
Jul 19, 2017
Atila Neves
Jul 19, 2017
Andrea Fontana
Jul 19, 2017
Seb
Jul 19, 2017
Jonathan Marler
Jul 19, 2017
ag0aep6g
Jul 19, 2017
Swoorup Joshi
Jul 19, 2017
Jonathan Marler
Jul 19, 2017
jmh530
Jul 20, 2017
Moritz Maxeiner
Jul 20, 2017
Iakh
Jul 19, 2017
Jonathan Marler
Jul 19, 2017
Cym13
Jul 19, 2017
Jack Stouffer
Jul 19, 2017
Seb
Jul 20, 2017
Iakh
Jul 20, 2017
Jack Stouffer
Jul 20, 2017
Moritz Maxeiner
Jul 19, 2017
H. S. Teoh
Jul 19, 2017
Jonathan M Davis
Jul 20, 2017
Olivier FAURE
Jul 20, 2017
Guillaume Piolat
Jul 21, 2017
Johan Engelen
Jul 24, 2017
Nick Treleaven
Jul 24, 2017
Nick Treleaven
Jul 24, 2017
Nick Treleaven
Jul 21, 2017
Jonathan M Davis
Jul 24, 2017
Olivier FAURE
July 19, 2017
So I was thinking of a way of extending if statements that have declarations. The following being as example of the current use of if statements with declarations:

    if(int* weDontPollute = someFunc())
    {
         // use weDontPollute
    }

That's great and all, but it only works by checking if the variable evaluates to true or false. Which is fine for a pointer but otherwise useless for anything else, like integers where zero is usually valid input (index for array). So currently the only way to do something like this in the language, that i've found, is to use a for statement.

    for(int i = someFunc(); i >= 0;)
    {
        // use i

        break;
    }

Not that ideal to use a for statement. It makes it hard to read and if the break condition isn't there it might very well be an infinite loop. So I was thinking of some sort of syntax like this:

    if(int i = someFunc(); i >= 0)
    {
        // use i
    }
Thoughts on this sort of feature?
July 19, 2017
On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
> Thoughts on this sort of feature?

I like it.
July 19, 2017
On Wednesday, 19 July 2017 at 13:37:50 UTC, Adam D. Ruppe wrote:
> I like it.

Me too. I think this should also apply to switch and with statements. Perhaps while statements too.
July 19, 2017
On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
> So I was thinking of a way of extending if statements that have declarations. The following being as example of the current use of if statements with declarations:
>
>     if(int* weDontPollute = someFunc())
>     {
>          // use weDontPollute
>     }
>
> That's great and all, but it only works by checking if the variable evaluates to true or false. Which is fine for a pointer but otherwise useless for anything else, like integers where zero is usually valid input (index for array). So currently the only way to do something like this in the language, that i've found, is to use a for statement.
>
>     for(int i = someFunc(); i >= 0;)
>     {
>         // use i
>
>         break;
>     }
>
> Not that ideal to use a for statement. It makes it hard to read and if the break condition isn't there it might very well be an infinite loop. So I was thinking of some sort of syntax like this:
>
>     if(int i = someFunc(); i >= 0)
>     {
>         // use i
>     }
> Thoughts on this sort of feature?

This is included in C++17: http://en.cppreference.com/w/cpp/language/if

```
int demo() {
   if (auto it = m.find(10); it != m.end()) { return it->size(); }
   if (char buf[10]; std::fgets(buf, 10, stdin)) { m[0] += buf; }
...
```
July 19, 2017
On Wednesday, 19 July 2017 at 14:26:52 UTC, Dukc wrote:
> On Wednesday, 19 July 2017 at 13:37:50 UTC, Adam D. Ruppe wrote:
>> I like it.
>
> Me too. I think this should also apply to switch and with statements. Perhaps while statements too.

Right, C++17 has it also for a switch:

```
If init-statement is used, the switch statement is equivalent to
{
init_statement
switch ( condition ) statement
}
```

http://en.cppreference.com/w/cpp/language/switch
July 19, 2017
On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
> So I was thinking of a way of extending if statements that have declarations. The following being as example of the current use of if statements with declarations:
>
> [...]

I like it for C++17 and I like it for D.

Atila
July 19, 2017
On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
> Thoughts on this sort of feature?

I think I read this topic a couple of times here, and it went nowhere...

Andrea

July 19, 2017
On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
> So I was thinking of a way of extending if statements that have declarations. The following being as example of the current use of if statements with declarations:
>
>     if(int* weDontPollute = someFunc())
>     {
>          // use weDontPollute
>     }
>
> That's great and all, but it only works by checking if the variable evaluates to true or false. Which is fine for a pointer but otherwise useless for anything else, like integers where zero is usually valid input (index for array). So currently the only way to do something like this in the language, that i've found, is to use a for statement.
>
>     for(int i = someFunc(); i >= 0;)
>     {
>         // use i
>
>         break;
>     }
>
> Not that ideal to use a for statement. It makes it hard to read and if the break condition isn't there it might very well be an infinite loop. So I was thinking of some sort of syntax like this:
>
>     if(int i = someFunc(); i >= 0)
>     {
>         // use i
>     }
> Thoughts on this sort of feature?


Achieving the same semantics as your example today would look like this

{
    int i = someFunc();
    if(i >= 0)
    {
        // use i
    }
}

The disadvantage being that it creates an extra level of nesting. I can see how this extra scope can discourage people from properly scoping their variables and this feature would eliminate the need for the extra scope.

Also note that this example shows how this could be implemented using "syntax lowering".

if( <declaration1> ; <desclaration2> ; ... ; <condition)
{
  //
}

Becomes:

{
    <declaration1>;
    <declaration2>;
    ...
    if(<condition>)
    {
        // ...
    }
}

July 19, 2017
On Wednesday, 19 July 2017 at 14:46:31 UTC, Andrea Fontana wrote:
> On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
>> Thoughts on this sort of feature?
>
> I think I read this topic a couple of times here, and it went nowhere...
>
> Andrea

Well someone must take the initiative and submit a DIP for this feature.
I miss it a lot too and would try to help this DIP.
July 19, 2017
On 07/19/2017 03:30 PM, sontung wrote:
> So I was thinking of some sort of syntax like this:
> 
>      if(int i = someFunc(); i >= 0)
>      {
>          // use i
>      }
> Thoughts on this sort of feature?

I'd prefer a new variant of `with`:

----
with (int i = someFunc()) if (i >= 0)
{
    // use i
}
----

It's slightly more verbose, but the meaning is clearer (arguable). It extends automatically to other control structures like `switch`.

I wouldn't have this new `with (declaration)` have the magic lookup rules of the existing `with (expression)`. It would be a simpler tool that I'd probably use more than the existing `with`.
« First   ‹ Prev
1 2 3 4