Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: If / while with multiple statements condition #13364

Closed
vbcodec opened this issue Aug 25, 2016 · 11 comments
Closed

Proposal: If / while with multiple statements condition #13364

vbcodec opened this issue Aug 25, 2016 · 11 comments

Comments

@vbcodec
Copy link

vbcodec commented Aug 25, 2016

Add ability to put multiple statements into condition for if and while.
Example:

if (
var p = GetPoint();
p.X += 10;
p.Length > 20)
{
...
}

Last statement (required) is expression that provide bool value.

Benefits:

  • better semantically corelated preparing and checking
  • less effort to avoid conflicts with names of temporaryy variables
@orthoxerox
Copy link
Contributor

There's an issue about "statement expressions" somewhere around.

@HaloFour
Copy link

This would be the "semi-colon expressions" that were proposed on CodePlex for C# 6.0 but were finally cut. Here's a mention: C# Design Notes for Dec 16, 2013

Those expressions would have extended to anywhere in the language:

bool b = (var p = GetPoint(); p.X += 10; p.Length > 20);

However considering the variable scoping of out and pattern variables to remain consistent the declared variable p would have to remain in the enclosing scope:

if ((var p = GetPoint(); p.X += 10; p.Length > 20)) {
   ...
}
// p is still in scope here

@HaloFour
Copy link

Here it is, Sequence Expressions: #6182

@vbcodec
Copy link
Author

vbcodec commented Aug 25, 2016

@HaloFour
The difference is that last statement (bool expression) allow for typeswitch, while #6182 probably not

@HaloFour
Copy link

@vbcodec

Is that in reference to the scope statement?

@vbcodec
Copy link
Author

vbcodec commented Aug 25, 2016

@HaloFour
Corrected, I meant #6182

@HaloFour
Copy link

@vbcodec

I don't see why sequence expressions would preclude type switch. The last statement in the sequence can be any expression, including a bool expression. Other than the fact that this proposal limits the sequence to within if and while conditions what differentiates it?

@alrz
Copy link
Member

alrz commented Aug 25, 2016

@HaloFour A pair of parentheses. I think the same thing for tuples is to be considered inside switch, default, method invocations etc, because double parentheses look confusing IMO,

This feature is also proposed for C++17 (link).

@HaloFour
Copy link

@alrz

That could be "fixed" by changing the spec to allow the if, while and do statements to accept a Boolean sequence expression directly:

if-statement:
if ( boolean-expression ) embedded-statement
if ( boolean-expression ) embedded-statement else embedded-statement
if boolean-sequence-expression embedded-statement
if boolean-sequence-expression embedded-statement else embedded-statement

Then both of the following would work:

bool b = (var p1 = GetPoint(); p1.X += 10; p1.Length > 20);
if (var p2 = GetPoint(); p2.X += 10; p2.Length > 20) {
   ...
}

@vbcodec
Copy link
Author

vbcodec commented Aug 25, 2016

@HaloFour
With #6182 you cannot use typeswitch while assigning value to variable, because you can write

bool b = (var p = GetPoint(); p.X += 10; p.Length > 20 && p is Point3d p3d);

which is incorrect. And that double braces.
Whils similar grammatically and semantically, they need different implementations.

@HaloFour
Copy link

@vbcodec

I don't see why not? A type-switch is just a Boolean expression that also assigns a variable. You can use it as follows:

bool b = p is Point3d p3d;

Of course p3d wouldn't be definitely assigned after that statement, but that's no different than various other situations with type switch:

if (x == y || p is Point3d p3d) {
    // oops, p3d not definitely assigned here
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants