Roslyn: Feature Request: make "let" completely synonymous with "var"

Created on 11 Jul 2016  ·  7Comments  ·  Source: dotnet/roslyn

Area-Language Design Discussion

Most helpful comment

OK, so you're saying you don't want a pattern-matching assignment statement, or a way of introducing readonly variables? You just want more keywords that do nothing new?

All 7 comments

OK, so you're saying you don't want a pattern-matching assignment statement, or a way of introducing readonly variables? You just want more keywords that do nothing new?

I'm not following your rhetoricals. I don't want more keywords as "let" already exists. And as far as I can tell, it serves the same function as "var" except just in LINQ statements.
My suggestion is to simplify the language by making these equivalent.

@micampbell

I'm not following your rhetoricals.

An expanded use of let has long since been proposed and worked into other larger language proposals. Specifically, pattern deconstruction and readonly locals. See Pattern Matching for C# - Destructuring Assignment. @gafter is probably trying to ascertain if you're proposal stands alone or is in reaction to those proposals.

And as far as I can tell, it serves the same function as "var" except just in LINQ statements.

Except that it's readonly, which does better align to one of the proposed forms of let above:

let x = 123;
x = 456; // compiler error

Okay, that's starting to make sense. I guess I should defer to you guys since you're computer language experts and since I just now poked my head in here with a random request.
but...
I am motivated by readability over compactness. If a local variable is meant to be readonly then maybe the readonly modifier should be added instead of using let. Again, I'm not as expert as you guys, but I have been coding a long time and love C#. I hate to see the scientific and engineering programming community drift to python just because it’s easier. Saying “let x = 1” is entrenched in mathematical thinking. It would be nice to use it synonymously with “var x = 1” and it (may) be bad if this implied readonly status.

today:

  • let x = 5 is readonly when it is used in linq query comprehension expressions
  • var x = 5 is mutable (can be changed), and cannot be declared readonly.

perhaps we could allow something like this in the future:

  • let x = 5 declares a readonly variable and can be used anywhere variable declarations are allowed, not just in linq query comprehension expressions.
  • readonly var x = 5 and const var x = 5 are allowed as alternatives to let for those that don't like writing type names twice, but still want to be explicit about the fact that a variable cannot be modified after the initial assignment.

@micampbell does this capture what you are thinking or am I missing something?

@micampbell

I am motivated by readability over compactness. If a local variable is meant to be readonly then maybe the readonly modifier should be added instead of using let.

That has been proposed as well. Having let be shorthand for readonly var was proposed for succinctness. #115

I hate to see the scientific and engineering programming community drift to python just because it’s easier. Saying “let x = 1” is entrenched in mathematical thinking.

Several languages have both var and let keywords which do different things, including JavaScript and Apple Swift. It's been proposed for Java as well, although they have not settled on the exact keywords. It's less usual for a language to have multiple keywords that do the exact same thing because that would be a largely pointless addition to the language.

I seriously doubt that anyone is flocking from C# to Python over whether or not the syntax matches mathematical notation, particularly something as insignificant as let variable declarations. Not a single language above Python on the TIOBE index has such syntax.

I am satisfied, and I concede. Thanks for the discussion and your time.
I hope others find this thread useful.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NikChao picture NikChao  ·  3Comments

joshua-mng picture joshua-mng  ·  3Comments

codingonHP picture codingonHP  ·  3Comments

marler8997 picture marler8997  ·  3Comments

vbcodec picture vbcodec  ·  3Comments