-
Notifications
You must be signed in to change notification settings - Fork 4.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
Proposal: Inferred type for const #4423
Comments
👍 I like this idea. It would also help with vertical alignment of constant names without having to resort to additional spacing: const subject = "Welcome :)";
const planPrice = 10M;
const months = 12;
const lastValidDate = new DateTime(2100, 1, 1); as opposed to: const string subject = "Welcome :)";
const decimal planPrice = 10M;
const int months = 12;
const DateTime lastValidDate = new DateTime(2100, 1, 1); Although, there is something to be said about being able to scan down a list of constants and easily be able to pick out ones based on type. |
I would love to have this kind of inference in method bodies so I can write
|
I have a PR for this here: #5050 Note: this is about supporting "const var x = ..." in a local scope. It does not include allowing "const x = ..." or allowing "const var x = " in a field of a class. |
See #5050 for a prototype of this. |
@CyrusNajmabadi The original proposal here was clearly intended to cover constant fields (two out of three examples are of that). Yet in your comment and PR it says "this is about supporting const var ... in a local scope". Can you please clarify whether the proposal is intended to support both, or is limited to the situations you have prototyped? If the latter, can you please edit the original proposal? |
Just to mention, LINQ uses from foo ...
from T foo ... There is even let foo = ...
let T foo = ... readonly locals, readonly foo = ...
readonly T foo = ... RAII,
So, const foo = ...
const T foo = ... It'll be actually more consistent without |
I was going to create an issue to request this feature, when I found this one. Did this happen with C# 7, and I just missed it? Was it dropped by the language team? Or did it's priority just get pushed down and so it's still on the back burner? From my perspective, it's local constants that I'd like to see: void F()
{
var x = 1;
const y = 2;
....
} But I really don't like @CyrusNajmabadi's PR that proposes Any updates from @gafter, @CyrusNajmabadi etc would be appreciated. |
@DavidArno This issue is still on the LDM's list of features to consider for future language versions. We're sorting and prioritizing that list over the next couple of weeks. When it stabilizes I'll publish it. |
That's good to hear. Thanks for the update. |
This feature request is now tracked at dotnet/csharplang#106. It is championed by @CyrusNajmabadi |
Background
Would be great to have a shorter way to define a constant. I think that defining the type is redundant and unnecessary.
Solution
Just define your constant without the type.
The text was updated successfully, but these errors were encountered: