Thread overview | |||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 03, 2016 If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
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? |
November 03, 2016 Re: If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry | On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry 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:
>
> [...]
Just Introduce another block
{
int i = someFunc();
if (i >= 0) { ... }
}
// i is not visible here
|
November 04, 2016 Re: If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Thursday, 3 November 2016 at 22:32:17 UTC, Stefan Koch wrote: > On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry 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: >> >> [...] > > Just Introduce another block > { > int i = someFunc(); > if (i >= 0) { ... } > } > // i is not visible here Introducing a block is not intuitive and cause an identation. As a reference, C++17 has adopted a proposal about this, and extended to switch: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r1.html |
November 04, 2016 Re: If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to mogu | On Friday, 4 November 2016 at 00:04:28 UTC, mogu wrote:
> Introducing a block is not intuitive and cause an identation.
How is using a block to produce a sub-scope not intuitive? That's using a standard feature to do exactly what it is supposed to do.
|
November 04, 2016 Re: If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Thursday, 3 November 2016 at 22:32:17 UTC, Stefan Koch wrote:
> Just Introduce another block
> {
> int i = someFunc();
> if (i >= 0) { ... }
> }
> // i is not visible here
That adds 2 indention levels after formatting.
Unfortunately this doesn't work:
---
{
int i = someFunc();
if (i >= 0):
//your code here
}
// i is not visible here
---
|
November 04, 2016 Re: If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Superstar64 | On Friday, 4 November 2016 at 13:38:30 UTC, Superstar64 wrote:
> On Thursday, 3 November 2016 at 22:32:17 UTC, Stefan Koch wrote:
>> Just Introduce another block
>> {
>> int i = someFunc();
>> if (i >= 0) { ... }
>> }
>> // i is not visible here
>
> That adds 2 indention levels after formatting.
>
> Unfortunately this doesn't work:
> ---
> {
> int i = someFunc();
> if (i >= 0):
> //your code here
> }
> // i is not visible here
> ---
If you don't like indentation you can simply ignore it or you can use old goto :)
{
int i = someFunc();
if (i < 0) goto outer;
// your code here
}
outer:
// i is not visibe here
|
November 04, 2016 Re: If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry | On 11/3/16 6:29 PM, Jerry 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?
Hm... what about something like:
struct BoolCond(T, string cond)
{
T val;
bool opCast(B)() if(is(B == bool))
{
return mixin("val " ~ cond);
}
}
auto boolcond(string cond, T)(T val)
{
return BoolCond!(T, cond)(val);
}
if(auto i = someFunc.boolcond!(">= 0"))
{
... // use i
}
-Steve
|
November 04, 2016 Re: If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jerry | On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry wrote:
> if(int i = someFunc(); i >= 0)
> {
> // use i
> }
> Thoughts on this sort of feature?
I would prefer the syntax
if( (int i = someFunc()) >= 0 )
{
// use i
}
as this matches the already existing assignment expression syntax if you pull the declaration out of the if expression.
|
November 04, 2016 Re: If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 11/4/16 10:10 AM, Steven Schveighoffer wrote:
> On 11/3/16 6:29 PM, Jerry 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?
>
>
> Hm... what about something like:
>
> struct BoolCond(T, string cond)
> {
> T val;
> bool opCast(B)() if(is(B == bool))
> {
> return mixin("val " ~ cond);
> }
Of course, I missed this:
alias val this;
-Steve
|
November 04, 2016 Re: If Statement with Declaration | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Friday, 4 November 2016 at 14:10:51 UTC, Steven Schveighoffer wrote:
> On 11/3/16 6:29 PM, Jerry 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?
>
>
> Hm... what about something like:
>
> struct BoolCond(T, string cond)
> {
> T val;
> bool opCast(B)() if(is(B == bool))
> {
> return mixin("val " ~ cond);
> }
> }
>
> auto boolcond(string cond, T)(T val)
> {
> return BoolCond!(T, cond)(val);
> }
>
> if(auto i = someFunc.boolcond!(">= 0"))
> {
> ... // use i
> }
>
> -Steve
Well that's just a basic case, what if you want more than one condition. Using "&&" for example, or a condition along with another value that's in the scope.
void otherFunc(int input)
{
if(auto i = someFunc.boolcond!(">= input")) // --- error
{
... // use i
}
}
Then you need another work around for something like this:
if(int value; auto i = someFunc(&value))
{
// ...
}
|
Copyright © 1999-2021 by the D Language Foundation