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: Inferred type for const #4423

Closed
KimTranjan opened this issue Aug 7, 2015 · 10 comments
Closed

Proposal: Inferred type for const #4423

KimTranjan opened this issue Aug 7, 2015 · 10 comments
Assignees
Labels
4 - In Review A fix for the issue is submitted for review. Area-Language Design

Comments

@KimTranjan
Copy link

Background

Would be great to have a shorter way to define a constant. I think that defining the type is redundant and unnecessary.

public const string Subject = "Welcome :)";

internal const decimal planPrice = 10;

const int months = 12;

Solution

Just define your constant without the type.

public const Subject = "Welcome :)";

internal const planPrice = 10M;

const months = 12;
@gafter gafter changed the title Proposal: Syntax sugar for const Proposal: Inferred type for const Aug 7, 2015
@gafter gafter added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Aug 7, 2015
@TyreeJackson
Copy link

👍 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.

@tpetrina
Copy link

tpetrina commented Aug 8, 2015

I would love to have this kind of inference in method bodies so I can write

const max = Config.MaxParams; // inferred as const Int32

@CyrusNajmabadi
Copy link
Member

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.

@gafter gafter added 2 - Ready and removed help wanted The issue is "up for grabs" - add a comment if you are interested in working on it labels Nov 20, 2015
@gafter
Copy link
Member

gafter commented Nov 20, 2015

See #5050 for a prototype of this.

@gafter gafter added 4 - In Review A fix for the issue is submitted for review. and removed 2 - Ready labels Nov 23, 2015
@gafter
Copy link
Member

gafter commented Dec 14, 2015

@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?

@alrz
Copy link
Member

alrz commented Dec 14, 2015

Just to mention, LINQ uses

from foo ...
from T foo ...

There is even CS1949 if you try to use var.

#6400:

let foo = ...
let T foo = ...

readonly locals,

readonly foo = ...
readonly T foo = ...

RAII,

using foo = ...
using T foo = ...

So,

const foo = ...
const T foo = ...

It'll be actually more consistent without var.

@DavidArno
Copy link

DavidArno commented Jan 17, 2017

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 const var y = 2;, the var is noise only I feel.

Any updates from @gafter, @CyrusNajmabadi etc would be appreciated.

@gafter
Copy link
Member

gafter commented Jan 17, 2017

@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.

@DavidArno
Copy link

@gafter,

That's good to hear. Thanks for the update.

@gafter
Copy link
Member

gafter commented Mar 27, 2017

This feature request is now tracked at dotnet/csharplang#106. It is championed by @CyrusNajmabadi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - In Review A fix for the issue is submitted for review. Area-Language Design
Projects
None yet
Development

No branches or pull requests

7 participants