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

Optional type information on local constants. #2713

Closed
davor-skontra opened this issue Aug 6, 2019 · 6 comments
Closed

Optional type information on local constants. #2713

davor-skontra opened this issue Aug 6, 2019 · 6 comments

Comments

@davor-skontra
Copy link

davor-skontra commented Aug 6, 2019

At the moment it is possible to prefix a local variable with var and the type information is inferred from the assignment:

For example:

private void SomeMethod() {
    var foo = 1;
}

However when declaring local constants it is necessary to also write a type. Like here

private void SomeMethod() {
   const float baz = 2f;

   var foo = 1;
}

This feels a bit inconsistent. Maybe const baz = 2f; should be allowed for local constants, that way we could write

private void SomeMethod() {
    const baz = 2f;

    var foo = 1;
}

This would be both more constant and convenient. Additionally it would encourage using immutable values when possible.

@ufcpp
Copy link

ufcpp commented Aug 6, 2019

#106

@Richiban
Copy link

Richiban commented Aug 6, 2019

See also #188 which would allow readonly locals (with a shorthand for readonly var) that would be applicable in all cases, not just constants.

@DavidArno
Copy link

And further, as per the discussion in #188, using let to be another way of expressing readonly var allows us to then say:

private void SomeMethod() 
{
   let baz = 2f; // readonly, ie constant
   let bar = new C(); // readonly, ie constant
   var foo = 1; // mutable
}

thus we get a consistent way of expressing local "constants" for all types, not just those allowed with the const keyword.

@john-h-k
Copy link

john-h-k commented Aug 9, 2019

Personally I don't see the value in this. Constant locals must be of type bool, byte, sbyte, ushort, short, uint, int, ulong, long, float, double, decimal, char, string. None of these are particularly long or complex keywords, and const modifier isn't used anywhere near as regularly as non const locals, so saving ~5 chars doesn't seem worth it

@HekiShavik
Copy link

But @john-h-k take a look at enums then:

const TravellerBase.TravellerType typeFilter = TravellerBase.TravellerType.Adult | TravellerBase.TravellerType.Youth;

versus

const typeFilter = TravellerBase.TravellerType.Adult | TravellerBase.TravellerType.Youth;

It's definitely worth it in my oppinion.

@YairHalberstadt
Copy link
Contributor

Closing as duplicate of #106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants