-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Champion "Const Var" #106
Comments
most of use cases of |
I don't believe there is any data that supports that statement. Teams (Roslyn-IDE for one) may just choose to use 'var' for all types where it can be used. |
I use var for all locals. |
@CyrusNajmabadi this feature is as simple as implement @jnm2 it's quite common to explicitly specify simple types. R# even has a rule for it I'm not saying it's completely useless, but here is bunch of much better champions. |
@Pzixel I'm aware of that, and I'm also aware that it's quite common not to specify simple types. I bemoan the fact that |
|
Working on PR here: dotnet/roslyn#21149 |
The I get why this isn't getting much attention because I only see the real benefit with enums:
versus
By the way, if |
Currently we can do Fields are in question, and generally speaking they should not be inferred for
But local inferred const values are a way to go: void Main()
{
const s = "Hello const!";
Console.WriteLine(s);
} That would be a great addition to C#. Personally I hit this omission several times a week. |
It's definitely not out of the question :). 'var' doesn't mean 'the value varies'. it means 'the type is inferred here'. So
|
Interesting fact: Rust has |
I find this apporach very useful. I don't care if it is const var or just const alone. |
An example: int x = 10;
const var finalResult = x + 5; Would this be a valid code? If not, what syntax would be used for immutable local variables if they ever appear in C#? Would it be E.g. int x = 10;
sealed var finalResult = x + 5; vs int x = 10;
let finalResult = x + 5; If you are OK with Alternatively, if you prefer to choose a concise Both approaches work. Here are some syntax samples for your convenience: // ------------------------------------------
// 1. const var / sealed var approach
// ------------------------------------------
const var s = "Hello";
int x = 10;
sealed int y = x + 1; // type is specified
sealed var z = x + 2; // type is inferred
// ----------------------------------------
// 2. const / let approach
// ----------------------------------------
const s = "Hello";
int x = 10;
let int y = x + 1; // type is specified
let z = x + 2; // type is inferred My personal vote goes for |
My favorite ist just const with ommitting its type. const acts as var in that way. |
See #188
|
My personal preference would be omitting the And please don't introduce any new unrelated keywords like |
How the runtime treats the "const [type]" expression and how it should "apear" to the developer in code is not necessarily the same. const without type could under the hood be compiled as const [type] as well. |
C# already treats them differently, |
I know. If const without type is limited to constant expressions, there is no reason not to ommit the type argument. the compiler can choose at design time which type it is. the things only get more difficult when the const value needs to be evaluated first. |
I see no reason why: const a = 3; cannot be compiled as if some wants a different type than the compiler chose, just type it. same applies for var, as well. |
Or if someone wants to 'declare' they're type-inferring they could choose to type out |
I wasn't commenting on that specific syntax decision, only that |
I aggree. Const for constant expressions, readonly for the others seems to address the mentioned dfficulties. |
Maybe 2023 will be finally the year of a const type inference in C#? Writing long enums is especially annoying: const DiscoveryOptions discoveryOptions = DiscoveryOptions.NoSort | DiscoveryOptions.Invariant; instead of a more natural and concise statement below: const discoveryOptions = DiscoveryOptions.NoSort | DiscoveryOptions.Invariant; As it already had been mentioned, JavaScript has this feature since ES6 (2015) and it causes exactly zero problems. Why C# is still lagging on this front? |
The keyword const has a very (very!) different meaning in JavaScript than it does in C#, plus the type systems are vastly different.
Every feature has costs and benefits - including the opportunity cost that working on one feature means a different feature doesn't get progressed. C# doesn't have this feature because there are other, more valuable features, that have been prioritised. That said, even in a mythical world of infinite resources, not every feature would make it into the language. The teams discipline in adding only the features that makes sense is to be applauded. It's worth noting that this features has a champion, putting it ahead of many other features as someone on the C# team has decided this is valuable enough to progress further. Even that's no guarantee of progress though. |
I have analyzers suggesting changing: var key = "Foo"; To: const string key = "Foo"; ... but it's just so verbose. I also think that, although const key = "Foo"; Are there any technical complications/ambiguities with this? Is this issue only blocked by available time and priorities? |
@colejohnson66 @Zodt What do you disagree with? |
See also dotnet/roslyn#4423
The text was updated successfully, but these errors were encountered: