If Statement with Declaration

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 19 11:46:39 PDT 2017


On 7/19/17 11:47 AM, Jonathan Marler wrote:
> 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.
>>
> 
> 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
>      }
> }

This is different. The variable exist outside the scope of the loop. 
This is more like a for-loop. Arguably, for-loops are better suited for 
this, and already support it:

for(auto a = ..., b = ...; something;)

An if statement runs once. There isn't an "exists for all the loops" for 
an if statement. So it's clean and neat to declare them in one place or 
the other. Only if you need to use it for the condition does the 
declaration have to be in the condition expression.

But arguably, for loops can be (and have been) abused, so this isn't 
exactly new territory. Like I said, I'm OK with the proposal if that's 
what gets it done.

-Steve


More information about the Digitalmars-d mailing list