-
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: Introduce let
as keyword for lazy constants
#7453
Comments
let
as keyword for lazy constants
There is no such thing as lazy evaluation in CLR. However, you can use var y = new Lazy<string>(() => x.Substring(0, 10)); And |
Furthermore, as a part of that decomposition spec, |
Although, there is a similar feature in F# as |
I think 'let' should be a compiler feature rather than CLR, so compiler could optimize |
#6400 and #117 cover most of my suggestion. However I suggest to make next step and have for(var i=0; i< 10; i++)
{
const int x = i; // Error CS0133 The expression being assigned to 'x' must be constant
// will be
let x = i;
...
} When we look at the application code, there are quite a few places, where Ultimately, it should lead to a for loop with for(let i=0; i< 10; i++) {...}
// or
foreach(let i in 0..10) {...} |
This is a side-effect of #6400, the destructured values would be immutable but can be the result of any expression. They would be evaluated immediately, though, which is necessary as the expression could have more side-effects than simply resulting in a value and it would be very unexpected if those side-effects did not occur at the point that the expression occurs lexically. Also, iteration variables are already immutable and thread-safe (per C# 5.0) so there is no need to extend |
Changing I would change |
@v-andrew can you give an example for |
@v-andrew The iterator variable must be mutable since the In Swift, Why should the syntax of |
for(let i=0; i< 10; i++)
{
i++; // Error
} I'm not sure that it looks good as i++ implies changes in i, so we could move to for(let i=0; i< 10; i + 1)
{
i++; // Error
} Life cycle of |
@HaloFour |
wa.. wait a minute. so, |
The
|
@alrz Yes, |
@HaloFour I do not suggest to remove |
@v-andrew, are you aware that you can initialize more than one variable in the
|
Why the heck we need new keyword just for lazy constant??? Why we don't just reuse const instead I mean instead of
We can just
And let const be known as shorthand const var. Problem solve Please don't add any new keyword so casually |
@paulomorgado Yes, I know it, however I see no relations to my proposal |
@Thaina "Why the heck we need new keyword just for lazy constant???" "Please don't add any new keyword so casually" |
@v-andrew It is keyword only inside query expression Same go for We should not bring keyword from lesser scope out of it scope. That's why I prefer |
We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages. |
C# already uses let clause in LINQ expressions.
Please extend use of let and make it to a general purpose keyword which declares lazy constants.
It looks organic and I see no confusing side effects.
Here are examples of 'new' syntax:
the same as
const x
const y
declared but not evaluated until it is used in expression.As it is considered to be a constant and has no side effects, compiler could optimize use of
y
.for example, when user takes length of
y
compiler can substitute it with10
The text was updated successfully, but these errors were encountered: