Champion "readonly for locals and parameters" #8479
-
Beta Was this translation helpful? Give feedback.
Replies: 1190 comments 143 replies
-
Any plan for readonly types(classes and structs)? |
Beta Was this translation helpful? Give feedback.
-
should support readonly i = 0; // shorthand for readonly var
const j = 0; // shorthand for const var |
Beta Was this translation helpful? Give feedback.
-
Wasn't |
Beta Was this translation helpful? Give feedback.
-
@jnm2 I just don't like the idea of adding new keyword. Especially it is already have keyword in the language that has the same meaning
At least I have seen some suggestion to use Especially because |
Beta Was this translation helpful? Give feedback.
-
@Thaina Yes, I'm inclined to agree. |
Beta Was this translation helpful? Give feedback.
-
And you could continue to. Like Although I do prefer |
Beta Was this translation helpful? Give feedback.
-
Oh yes! |
Beta Was this translation helpful? Give feedback.
-
@HaloFour It not breaking change I understand but it still ambiguous BTW I still don't like |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Personally the latter reason is enough for me. It's already a contextual keyword, and in that existing context it creates a readonly identifier. |
Beta Was this translation helpful? Give feedback.
-
Fair enough 😄 |
Beta Was this translation helpful? Give feedback.
-
Do you mean immutable types? If so, that's a completely separate proposal (I'm pretty sure it's been made before). |
Beta Was this translation helpful? Give feedback.
-
ITNOA @Richiban where I can found it? |
Beta Was this translation helpful? Give feedback.
-
That's a new one! dotnet/roslyn#7626 and https://github.com/dotnet/roslyn/issues/159 are probably what you're looking for. |
Beta Was this translation helpful? Give feedback.
-
Something I like about final String name;
if (entity instanceof Person) {
Person person = (Person)person;
name = person.getFirstName() + " " + person.getLastName();
}
else if (entity instanceof Company) {
Company company = (Company)company;
name = company.getFirmName();
} This can be useful in those scenarios where you want the local to be readonly, the expression to calculate it can throw and you want the scope of the local to exist beyond a |
Beta Was this translation helpful? Give feedback.
-
The language team disagrees with you. It's an additional modifier which, for the folks that want this feature, would end up added to the vast majority of all declarations. That is noisy. The fact that it's 3 characters instead of 8 doesn't really change that. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour If the alternative is no readonly for locals at all, I would prefer "noisy" 3 characters. Perfect is the enemy of good. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
The alternative is an analyzer, so you can choose whether you want locals to be treated as readonly by default, as an analyzer is allowed to make decisions that the language won't. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Analyzer is a poor man's alternative (which is, well, better than nothing). This alternative would create a non-standard language dialect, developers will need to learn how to apply the needed analyzer, how to fight its bugs, how to cancel the readonlyness for cases where it's not intended and so on. In some organizations this would create an additional SOUP item. |
Beta Was this translation helpful? Give feedback.
-
It doesn't create a dialect, it limits what you can do within the standard language. This is no different from StyleCop or any of the other tools routinely used by organizations to enforce code hygiene. It's infinitely cheaper than a language change and addresses all of these concerns, including the ability to flip the default, or to enforce in some situations and not others. |
Beta Was this translation helpful? Give feedback.
-
In the interest of making the language team's position on this clear, we've decided to close this issue, mark it |
Beta Was this translation helpful? Give feedback.
-
This got me thinking, are there arguments for or against (in an ideal world of writing C# from scratch with today's knowledge) making fields readonly by default? My thinking is, there is a benefit to consistency, if we agree that locals and params are readonly by default (in an ideal world), kind of makes sense for fields to be the same. I'm sure there are reasons why that line of thinking is wrong and I'd like to hear them. |
Beta Was this translation helpful? Give feedback.
-
There are my arguments for:
How is this possible? This is without any empirical evidence. The fact that this works very well in Java, this line of argument disproved by as it is contradictory to the real world uses.
Also for immutability by default will not fly as I have seen many people suggest else where. The current language is mutability by default. This should stay as it is. Again this works well for Java, so I don't see a problem or argument why this will be problem for C#. Also immutable at the CLR level will open possibilities for its use and optimisation in languages other than C# too. NB: Moved from #8478 |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
Another thing is perhaps |
Beta Was this translation helpful? Give feedback.
-
Can you summarize, what's the drawback of making "readonly by default" a project setting, same as nullable? Too much confusion? |
Beta Was this translation helpful? Give feedback.
-
Since this issue has been closed as 'Likely never', is the team open to splitting a LINQ-matching I ask because I too am of the opinion that However, "readonly" locals are still a worthwhile addition to the language and they have the advantage of 0% extra noise in the majority of cases. The IDE feature released a few years ago, where mutated variables can be highlighted in the editor, is very useful for readability in the IDE but, perhaps ironically, a great deal of code is read not in the IDE but in a diff or PR where only basic syntax highlighting is normally available. |
Beta Was this translation helpful? Give feedback.
-
Looks like an analyzer is our best option right now. Has anyone designed what syntax would be ideal for this? Maybe if we explore the analyzer route, it might change opinions. If successful, we all can move on. Just use the analyzer. However, if the analyzer approach is fully explored and can't provide a reasonable solution, maybe the language team will reconsider. Analyzer thoughts:
|
Beta Was this translation helpful? Give feedback.
In the interest of making the language team's position on this clear, we've decided to close this issue, mark it
Likely Never
, lock it, and are moving the existing comments over to a discussion. It is our position at this time thatreadonly
locals are not going to be added to the language; we do not feel that the noise from addingreadonly
as a local modifier, which will then need to be applied to most local variables, is worth it for the benefits that such a change would bring. We also do not believe that benefits here would be worth introducing a compiler switch or language dialect.readonly
on parameters is still an open question, and we will track that specific proposal here. If we so…