Roslyn: Proposal: Inferred type for const

Created on 7 Aug 2015  路  10Comments  路  Source: dotnet/roslyn

Background

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

``` c#
public const string Subject = "Welcome :)";

internal const decimal planPrice = 10;

const int months = 12;

# Solution

Just define your constant without the type.

``` c#
public const Subject = "Welcome :)";

internal const planPrice = 10M;

const months = 12;
4 - In Review Area-Language Design

Most helpful comment

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.

All 10 comments

:+1: 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

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

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

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:
cs 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.

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

@gafter,

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

This feature request is now tracked at https://github.com/dotnet/csharplang/issues/106. It is championed by @CyrusNajmabadi

Was this page helpful?
0 / 5 - 0 ratings