Champion "Const Var" #8663
Replies: 28 comments 1 reply
-
most of use cases of |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
I use var for all locals. |
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Working on PR here: dotnet/roslyn#21149 |
Beta Was this translation helpful? Give feedback.
-
The I get why this isn't getting much attention because I only see the real benefit with enums:
versus
By the way, if |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
It's definitely not out of the question :). 'var' doesn't mean 'the value varies'. it means 'the type is inferred here'. So
|
Beta Was this translation helpful? Give feedback.
-
Interesting fact: Rust has |
Beta Was this translation helpful? Give feedback.
-
I find this apporach very useful. I don't care if it is const var or just const alone. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
My favorite ist just const with ommitting its type. const acts as var in that way. |
Beta Was this translation helpful? Give feedback.
-
See #188
|
Beta Was this translation helpful? Give feedback.
-
My personal preference would be omitting the And please don't introduce any new unrelated keywords like |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
C# already treats them differently, |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
Or if someone wants to 'declare' they're type-inferring they could choose to type out |
Beta Was this translation helpful? Give feedback.
-
I wasn't commenting on that specific syntax decision, only that |
Beta Was this translation helpful? Give feedback.
-
I aggree. Const for constant expressions, readonly for the others seems to address the mentioned dfficulties. |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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? |
Beta Was this translation helpful? Give feedback.
-
@colejohnson66 @Zodt What do you disagree with? |
Beta Was this translation helpful? Give feedback.
-
I think we should |
Beta Was this translation helpful? Give feedback.
-
See also dotnet/roslyn#4423
Beta Was this translation helpful? Give feedback.
All reactions