Generic constraint where T : readonly struct
#8753
Replies: 7 comments
-
@AustinBryan How would this interact with existing compilers and runtimes? |
Beta Was this translation helpful? Give feedback.
-
Maybe you can omit the 'struct' keyword from the constraint and only write 'readonly'.
readonly is (for now) only possible for structs. |
Beta Was this translation helpful? Give feedback.
-
Is there any progress on this? What is language team's opinion? |
Beta Was this translation helpful? Give feedback.
-
Just restricting to |
Beta Was this translation helpful? Give feedback.
-
Could you please explain why a |
Beta Was this translation helpful? Give feedback.
-
It would completely miss that information for any other type which might be mutable but only for specific methods. For example, If you are looking to avoid the defensive copies, an analyzer like ErrorProne.NET will solve the same problem, but will correctly flag all the scenarios without needing to touch the language. |
Beta Was this translation helpful? Give feedback.
-
Sure, that makes perfect sense for that example. And in my example, my assumption is that avoiding the defensive copies would essentially amount to making overloads for each |
Beta Was this translation helpful? Give feedback.
-
Right now, doing this doesn't work:
This would be useful because, when using
in
parameters and larger structs, there are performance impacts from using thein
parameter due to the defensive copy. This doesn't happen withreadonly
structs.Therefore, it would be useful constraint so we know, in the scope of the class or method, we can use
in
parameters and benefit from the performance gains.Beta Was this translation helpful? Give feedback.
All reactions