-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Mark Value and HasValue of System.Nullable<T> as readonly #836
Comments
Since |
C# added But, basically, this means we would do the following:
This would mark (and enforce) that We have https://github.com/dotnet/corefx/issues/36586 as the meta tracking issue for updating framework APIs, in general, to use the new feature. |
What about marking the entire struct |
We can't do that as T may itself be mutable (and therefore calling We had actually attempted to mark the entire struct readonly previously and had to revert it. |
For reference: dotnet/coreclr#15198 |
Hello I think the data stored in the nullable should never change. If you assign a non nullable to a nullable it should create a new nullable object which replaces the old one. No change of the old nullable. If you call something like ToSting on the wrapped object than it should create a temporary for that call (ToString should be readonly too). It is the same like with int, int is readonly. You can’t change the number one. You can only replace that number with a different value. |
While it might be nice if that was the behavior, that isn't the current behavior and hasnt been the behavior since It also isnt the only struct which exists which needs to be partially mutable and is part of the reason we have readonly instance members (which allows certain methods to be readonly and avoid hidden copies). |
Unfortunately we have as far as I know currently no possibility to overload on readonlyness (like in C++) on member functions. The readonly modifier is/was intended for perfomance optimization. But in this case it causes the opposite. Every call to an readonly nullable field causes a copy... |
This would be a really nice change since it would enable passing nullable structs through
Can I submit a PR for this, or does this need to be triaged/validated first? |
We've taken past changes through API review. |
API review - it looks like the Roslyn compiler already special-cases this, so perf is already in a good way. But we should probably do this anyway for non-Roslyn compiler scenarios, and so that the API surface matches the expectation. Approved for these APIs specifically:
|
I've marked this as |
Great, I'll sumbit a PR then since I offered to do it. Also, how should netstandard be handled? |
For now, you will just need to update the implementation and the reference assembly. Any netstandard changes would happen in the next netstandard release (which I believe @terrajobst is the best to provide details on). |
accessing Value and HasValue of System Nullable in a readonly context causes a copy...
=> please mark these properties readonly
(best would be a static analyzer which marks as much readonly as possible)
The text was updated successfully, but these errors were encountered: