-
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: let as the read-only counterpart of var #16182
Comments
On the whole, I support this proposal but, I have a few reservations.
I disagree. From the callers point of view, I suspect that the desire to use void Log(let string x) => Console.WriteLine(x); just looks stupid. Regardless, I want to reiterate that whatever the token is, it should not be surfaced to callers as it is the definition of an implementation detail. |
I'm confused. This has nothing to do with callers, whatsoever.
We already have a |
Indeed that was the point I was making. You say that parameters are more high profile and should use the |
In the past I was in favour of |
so basically c++ const ? |
@timopomer Can you elaborate? why do you think we don't need this in C#? we already have p.s. This will probably make the cut for the next version of C# this post is more about the syntax. |
That's why I prefer My preference is still As for anything explicitly-typed such as parameters I think |
Then if you want to make a variable read-only, it would depend on whether it is explicitly-typed or not, and you may need to change the modifier. Also |
@eyalsk Also does it need its own keyword? wouldnt an implementation like
And what about unsafe code? This would require more than just a compiler change, the JIT would need to be modified to make this work if im not wrong and what if a variable is once passed as let and once normally and the normal variable is changed. do we get an exception? Is it worth spending time on? i dont know. im just creating questions here that need answering |
In
Because it looks better in all places and more consistent with
IDE can improve the experience by providing a different color, I think that this would be sufficient.
Why we need to be as verbose as Java? when we have opportunities to make it better? |
We'll have to agree to disagree there. I don't think that
I have to scan code in git repos online frequently enough for me to have a strong opinion against that languages that rely on IDE syntax highlighting/coloring to be legible.
Same number of tokens. A couple of extra characters. The argument of verbosity is so thin there that I don't think that it applies. Are you also suggesting that The void Foo(readonly MyClass obj) {
obj.Value = "12345"; // legal
obj = new MyClass(); // not legal
} The feature exists specifically to allow a function author to prevent accidentally overwriting variables. It doesn't have any effect on callers. |
In that case all this should be is a sign that you cannot assign anything to the let? as if the "=" operator would be overloaded and gave a compiler error when attempted to be used? I guess thats ok, but i dont think it would give us any form of optimization |
@timopomer It does not have anything to do with optimizations, specifically that Roslyn generally do not do optimizations and it is usually deferred until runtime. |
You're wrong the C++11 specification does not say anything about So if you're consuming something from the STL you can have some guarantees about thread-safety given that your own objects satisfy the requirements. You can watch the video by Herb Sutter to verify this In C#
I don't know what
Don't know, don't care, this is something that the design team need to decide on.
Again, we're not speaking about implementation here so whatever it takes. |
The consistency doesn't lie in their shared letters but in the fact that they complement one another and by that I mean one represents a variable that can change and the other represents a value that cannot change. Now, I agree that this isn't a reason to tipped the scales, I'm just stating my opinion here.
Fair point, in fact, I think I wrote this exact reason back then but dunno I changed my mind and the more I think about it I just like
No, I don't suggest that |
I don't know that the argument still stands but it had been mentioned in comments earlier that Given the IDE argument, you'd be typing the same amount of characters to add either Either way, that's also just my opinion, and it's not that strong of an opinion. Certainly not my hill to die on. I think it's wise to start with |
@HaloFour Why do you think we should start with something that requires a shorthand in the first place? |
@alrz Same reason preoptimization is bad. |
|
It pushes more complexity to the syntax and the compiler rather than simplifying things. It doesn't really make sense to me. It's mentioned before that anonymous methods were a mistake. But that was when the actual use cases were not identified. This is just like that except now we know it will be very common to use read-only locals. |
Readability is significantly more important than either syntax or compiler complexity. Humans have to read and parse that code too. I'm also of the opinion that readonly locals will end up being relatively rare. A tool to make functional developers feel more at home at best. Java only required it to make closures simpler but as of 8 they made the keyword optional. So you intend for this proposal to supplant #115 rather than augment it? |
The reason I'm so torn on this is because I think that what you propose make sense for the short-term but not the long-term if in the long-term it's going to be viral enough to justify a syntactic sugar and we're going to choose either |
I disagree. you should always use read-only locals unless they are not read-only. Actually it will be a good candidate for an analyzer to suggest to make locals read-only if they are not assigned to, exactly because humans have to read and parse that code. It's a good practise in JS too that you use
I made this proposal considering the overlap it entails with pattern-matching statement (#6400). See the disclaimer at the beginning of the OP. |
What does "short-term" or "long-term" have to do with anything?
Indeed, a mindset of functional programming. I even agree with it, for the most part. But I don't think that's particularly relevant. When it comes to adoption existing C# programmers aren't likely to reach for something new that doesn't buy them some obvious benefits that makes their immediate lives easier. This won't reduce the amount of code that anyone needs to type and the argument that it makes code more maintainable is an abstract one.
The #6400 proposal is dead in the water. It relied on pattern variables being immutable which is no longer the case. Use of it with readonly locals was also only a form of shorthand for |
Unless it is just moved to pattern-matching spec draft?
The form
The two proposal are irrelevant to each other (pattern-matching and readonly locals). But they can be used together e.g.
How is that a mindset of functional programming and from when it is a taboo to adopt functional programming's good practice?
That was your own argument, because humans have to read that code? You claim verbosity is irrelevant to the readability but also using a read-only local in the first place is irrelevant too?
It does if you are willing to use read-only locals. At this point I'm positive your password is leaked. |
That proposal died when the scoping rules and mutability of pattern variables changed. The spec hasn't been changed to reflect that nuclear bomb but I expect what is proposed for C# 8.0 will barely resemble it. I make the argument that any and all code should be easily readable, based on it's grammar. That has nothing to do with my assertion that readonly locals will find limited adoption amongst the C# audience. This isn't because readonly locals are bad or that its somehow wrong to follow functional practices. I just don't think most C# programmers will care and I don't think you'll be able to convince . I think that the changes to the pattern matching spec regarding the mutability of pattern variables reflects this reality. |
What I meant by short-term and long-term is if after some time
I wasn't speaking about So to make it crystal clear what I meant is as follow:
Introducing val early as opposed to having it in a future version.
|
We are not contemplating any additional feature work for C# 7. |
@gafter This is not a proposal for C# 7, although I think there will be a compat-issue similar to #13148. If read-only locals are planned for a point release, (per #16155 (comment)) wouldn't it make sense to settle that at this point? |
@alrz There are currently no such plans (for read-only locals or readonly refs) for a point release. We have a longish list of things we might or might not do in the future. readonly refs has been tentatively categorized by the @dotnet/ldm to be considered for possible inclusion in the timeframe of C# 8 (i.e. not a point release). I suspect @jaredpar was hinting that he has other ideas for prioritizing that work that may change when we consider them. That feature hasn't been designed yet; it is an open question whether the relevant keyword would be I do not agree with comments on that thread that a |
I agree. I'd like to see |
In that comment I was referring to |
Could this list be published please and maintained in the open, so we all have up to date info on the team's thinking on this matter? |
@DavidArno I'll ask around about that. |
How much does that list align with the proposals on this repo? I don't see one specifically for "readonly ref". There are already more than 50 issues with the "Ready" tag and well over 400 others queued up behind that. It'd be nice to know that there is some motion behind those proposals rather than them being semi-permanently stuck in limbo. |
Well, this list might be a good start: lots of pattern matching, records/ADTs, but nothing about immutability or non-nullability. I wish to know if they are still considered for vNext as well. |
@orthoxerox I think this one is more reliable, however, it doesn't look comprehensive or longish. |
Disclaimer: This is particularly related to #16183 as the syntax has some overlaps with previously proposed pattern-matching statement (#6400). Also, I think if this is decided now, it might help if we prevent some syntactical forms to retain backward compatibility in the future (#13148).
Considering how common it will be to define read-only locals,
readonly
is too verbose to be used as a modifier on local variables (#115). Instead, I suggest we uselet
as the read-only counterpart ofvar
:Some statements like
foreach
andusing
define read-only variables by default. That could be the case for an analyzer to suggest to uselet
in those places to make that clear, visually.However, parameters should use
readonly
just like fields, as they are more high-profile than locals.Note, unlike
var
,let
is able to be used as a modifier on explicitly-typed locals,Notice that this is ambiguous with pattern-matching statement (#6400). See #16183 for more on that.
The text was updated successfully, but these errors were encountered: