July 19, 2017
On 7/19/17 9:30 AM, 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?


I really like the idea. Only thing I don't like is the possibility for abuse/confusion/errors:

if(int i = someFunc(); j >= 0) // typo, or weird relationship, or just intentional obfuscation?

It reminds me a bit of why we got rid of the comma operator.

This is why I've liked suggestions in the past like:

if((int i = foo()) >= 0)

That is, you want to use 'if' on an expression while saving the expression, but the if is only looking at a property of that expression.

Note this makes if(arr) (the correct meaning, that is ;) much more palatable:

if((auto x = getArray()).length)

Don't get me wrong, if this syntax is what gets this idea in, I'm fine with it.

One possibility is to require usage of the declared variable in the condition.

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

To be frank, I don't think that helping the programmer reduce the line count in their program by one line is worth further complicating the language.
July 19, 2017
On Wednesday, 19 July 2017 at 15:39:02 UTC, Steven Schveighoffer wrote:
> On 7/19/17 9:30 AM, 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?
>
>
> I really like the idea. Only thing I don't like is the possibility for abuse/confusion/errors:
>
> if(int i = someFunc(); j >= 0) // typo, or weird relationship, or just intentional obfuscation?
>
> It reminds me a bit of why we got rid of the comma operator.
>
> This is why I've liked suggestions in the past like:
>
> if((int i = foo()) >= 0)
>
> That is, you want to use 'if' on an expression while saving the expression, but the if is only looking at a property of that expression.
>
> Note this makes if(arr) (the correct meaning, that is ;) much more palatable:
>
> if((auto x = getArray()).length)
>
> Don't get me wrong, if this syntax is what gets this idea in, I'm fine with it.
>
> One possibility is to require usage of the declared variable in the condition.
>
> -Steve

I respectfully disagree with this.  I recall many times where I want to declare variables that should be limited to the scope of the conditional block but aren't used in the condition itself, i.e

{
    auto a = ...;
    auto b = ...;
    while(something)
    {
        // use a and b
    }
}

I imagine this feature allowing it to be rewritten as:

while(auto a = ...;
      auto b = ...;
      something)
{
    // use a and b
}


July 19, 2017
On Wednesday, 19 July 2017 at 15:39:02 UTC, Steven Schveighoffer wrote:
> On 7/19/17 9:30 AM, sontung wrote:
>> [...]
>
>
> I really like the idea. Only thing I don't like is the possibility for abuse/confusion/errors:
>
> if(int i = someFunc(); j >= 0) // typo, or weird relationship, or just intentional obfuscation?
>
> It reminds me a bit of why we got rid of the comma operator.
>
> This is why I've liked suggestions in the past like:
>
> if((int i = foo()) >= 0)
>
> That is, you want to use 'if' on an expression while saving the expression, but the if is only looking at a property of that expression.
>
> Note this makes if(arr) (the correct meaning, that is ;) much more palatable:
>
> if((auto x = getArray()).length)
>
> Don't get me wrong, if this syntax is what gets this idea in, I'm fine with it.
>
> One possibility is to require usage of the declared variable in the condition.
>
> -Steve

Now that I think about an extension of the comma makes more sense:

    if (int i=foo(), i<42) ...

It is exactly the idea that we are trying to convey here (except that, deprecated comma aside, it doesn't work because of scope issues: i in i<42 is considered undefined).

Given that the comma will never die and that it conveys exactly the idea I think think it should be prefered to the semi-colon.
July 19, 2017
On 7/19/17 11:41 AM, Jack Stouffer wrote:
> On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
>> Thoughts on this sort of feature?
> 
> To be frank, I don't think that helping the programmer reduce the line count in their program by one line is worth further complicating the language.

It's 3 lines. One for the extra scope, one for the declaration, and one for the closing scope.

Hm... just had an idea:

auto propertyCheck(string condition, T)(T val)
{
    static struct Result {
       T _value;
       opCast(B: bool)() {
          mixin("return _value " ~ condition ~ ";");
       }
       alias _value this;
    }
    return Result(val);
}

if(auto x = someFunc().propertyCheck!" >= 0")
{
   // use x as if it was the result of someFunc()
}

Has some drawbacks, for instance you may want to use the true booleanness of whatever T is inside the function.

-Steve
July 19, 2017
On Wednesday, 19 July 2017 at 15:41:18 UTC, Jack Stouffer wrote:
> On Wednesday, 19 July 2017 at 13:30:56 UTC, sontung wrote:
>> Thoughts on this sort of feature?
>
> To be frank, I don't think that helping the programmer reduce the line count in their program by one line is worth further complicating the language.

As Steve mentioned, it's three lines and apart and it makes a huge difference if you have to review a large codebase. After all, syntactic sugar is one of the reasons why Python is so popular.
July 19, 2017
On Wednesday, 19 July 2017 at 15:31:08 UTC, ag0aep6g wrote:
> 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`.

I really prefer this over if. This just made sense, just at a glance
July 19, 2017
On Wednesday, 19 July 2017 at 16:13:28 UTC, Swoorup Joshi wrote:
> On Wednesday, 19 July 2017 at 15:31:08 UTC, ag0aep6g wrote:
>> 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`.
>
> I really prefer this over if. This just made sense, just at a glance

Me too.
Would also allow

with(char c = foo()) switch(x)
{
   // use c
}

so no need to switch over the new declared variable, but still beeing able to use it there
July 19, 2017
On Wednesday, 19 July 2017 at 16:13:28 UTC, Swoorup Joshi wrote:
> On Wednesday, 19 July 2017 at 15:31:08 UTC, ag0aep6g wrote:
>> 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`.
>
> I really prefer this over if. This just made sense, just at a glance

Could also make 'with' behave like 'if', where it could apply to a block or just a single statement, i.e.

// 'if' examples
if(x)
    doSomething

if(x)
{
    doSomething
}

// 'with' examples
with(auto x = 0)
    doSomething

with(auto x = 0)
{
    doSomething
}

This would automatically make it work with any statement:

with(auto x = 0) if(x)
{
   doSomething
}

with(auto x = 0) while(x)
{
   doSomething
}

Could also do multiple with statements

with(auto x = 0)
with(auto y = 0)
if(check(x, y))
{
    doSomething
}

July 19, 2017
On Wednesday, 19 July 2017 at 16:49:38 UTC, Jonathan Marler wrote:

> This would automatically make it work with any statement:
>
> with(auto x = 0) if(x)
> {
>    doSomething
> }
>
> with(auto x = 0) while(x)
> {
>    doSomething
> }
>
> Could also do multiple with statements
>
> with(auto x = 0)
> with(auto y = 0)
> if(check(x, y))
> {
>     doSomething
> }

Yes. That's exactly the idea. Only with this synax it's worth it.