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

Variable Aliasing "Out var" style #899

Closed
TonyValenti opened this issue Sep 12, 2017 · 8 comments
Closed

Variable Aliasing "Out var" style #899

TonyValenti opened this issue Sep 12, 2017 · 8 comments

Comments

@TonyValenti
Copy link

Hi C# Team,
I love how you can create, assign, and use variables with out var in situations like this:

if(int.TryParse(StringValue, out var x) && x >= 21){
  Console.WriteLine("Have a beer!");
}

I would like to propose that the language be extended to allow similar constructs where ever variables are used. Specifically comparison evaluations.

Here are some examples of what I would like to be able to do:

if(Company.CEO.Age CEOAge >= 21  && CEOAge < 45){
  Console.WriteLine("This example assigns CEOAge the value of Company.CEO.Age");
}

if(Company.Financials.CalculateIncome(x,y,z) CompanyIncome > Threshhold){
  Console.WriteLine($"The company made {CompanyIncome} which exceeded {Threshhold}");
}

I think that scoping rules should be identical to Out-Var.
This has the following benefits:

  • I don't have to create variables outside of my scope in order to access them.
  • Aliases allow performance benefits because the value is evaluated once and reused.
  • Cleaner code.
@HaloFour
Copy link
Contributor

You can use is var:

if (Company.CEO.Age is var CEOAge && CEOAge >= 21 && CEOAge < 45) {
}

@jnm2
Copy link
Contributor

jnm2 commented Sep 12, 2017

@HaloFour beat me by seconds :D

@Logerfo
Copy link
Contributor

Logerfo commented Sep 12, 2017

@HaloFour as far as I know, is var always returns true. If that's right, it's weird that the compiler still flag checks it: https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+ABADAAmwEYBuAWAChsBmQgJkKIHZ8BvS/Tw24gNkIAsjABwAKAJRsu0mdI6z8ASwBmohoqj4AbhAT4AXuPkKuAYQD2AOyjmANnAB0AdQSL4owyRPH8AX0q+QA==

@DavidArno
Copy link

@HaloFour

You can use is var:

Or, using my suggestion of an "as pattern":

if (Company.CEO.Age as var CEOAge >= 21 && CEOAge < 45) {
}

(which is basically the same as this suggestion and neatly avoids the need to introduce the variable and perform the comparison as two separate expressions).

@Logerfo
Copy link
Contributor

Logerfo commented Sep 12, 2017

@svick if I build a solution in VS with debug variant, it will still use the release version of the compiler, right?

@svick
Copy link
Contributor

svick commented Sep 12, 2017

@Logerfo How you compile the compiler doesn't matter. What matters is what you tell the compiler to do. If you tell it to compile your code in Debug mode, it produces less optimized (and easier debuggable) IL, which in this case includes the unnecessary flag check.

And the Debug/Release option in SharpLab has the same effect as the Debug/Release option in VS.

@Logerfo
Copy link
Contributor

Logerfo commented Sep 12, 2017

@svick thank you very much

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

7 participants