Champion "readonly for locals and parameters" #8479
-
Beta Was this translation helpful? Give feedback.
Replies: 1185 comments 96 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.
-
I would argue that shallow immutablity (readonlyness) is useful mostly for the developer, in order to protect from silly mistakes. As far as yesterday I found in my code a mistake: I was modifying a wrong local variable by mistake inside some if-branch. Readonly would have prevented this bug. So the advantage is not efficiency but ease of reasoning about the code. |
Beta Was this translation helpful? Give feedback.
-
Right. The issue is that we don't see this mistake happening much in the wild. As such, making a heavyweight, noisy, feature like this to prevent it is not palatable. We recommend that if this is not something you want, you write an analyzer for this purpose. |
Beta Was this translation helpful? Give feedback.
-
Well, I cannot speak for the majority of C# users, but I find myself often in a position where I would prefer having readonlyness available. It must not be necessarily noisy, a simple |
Beta Was this translation helpful? Give feedback.
-
That scenario is more common than you think |
Beta Was this translation helpful? Give feedback.
-
Which can only ever solve for one particular use case, namely inferred locals. It doesn't help with parameters, and it doesn't help for anyone who prefers to not use type inference. There are no options to make it not noisy from the language perspective, aside creating a dialect. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
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.
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…