Disallow non-readonly methods and properties on "in" value types #4872
Replies: 3 comments 1 reply
-
https://github.com/SergeyTeplyakov/ErrorProne.NET is one possible solution as it will flag many types of hidden copies. I'd appreciate if we could get a warning wave for this as well since it can be difficult or near impossible for analyzers to catch every kind of hidden copy. |
Beta Was this translation helpful? Give feedback.
-
This would be a breaking change, and the LDM have historically been extremely reticent to take those on. Except in extreme cases, upgrading the compiler should not break existing code that worked. This is not an extreme case. Using an Analyzer is the correct solution for this situation because it allows you to opt into the very specific rules you want for your situation without imposing them as a tax on the vast majority of project that just don't care. |
Beta Was this translation helpful? Give feedback.
-
I think it would be fine to catch these either with a community analyzer, or an analyzer built-in to the SDK which is controlled by a warning wave, or in the compiler itself in a warning wave. cc @jmarolf |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The
in
keyword is very close to being incredibly useful. Unlikeref
, it allows you to implement interfaces (likeIComparable
, etc) on structs without making copies, which is great for large structs, etc. However, it has a major flaw in that it is very easy for it to silently cause methods which take structs viain
to become more expensive (in terms of the number of copies) than just passing the struct by value (withoutin
).It's incredibly easy to forget to mark properties and methods as
readonly
, especially in a large code base with many developers working on different aspects, and thus cause the compiler to create defensive copies without any warning or indication.If the entire point of the
in
keyword for value types is to avoid making copies when it's not necessary, would it not be better to just fail to allow programmers to call non-readonly
methods and properties on astruct
passed via thein
keyword.Beta Was this translation helpful? Give feedback.
All reactions