4 ms·
readonly ref/in params in C#7.2? https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/readonly-ref.md https://github.com/dotnet/csharplang/blo
by benaadams 9y ago
readonly ref/in params in C#7.2?
https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/readonly-ref.md https://github.com/dotnet/csharplang/blob/master/proposals/c...
- polskibus 9y agoIt seems like that's a step in the right direction, although as I can see implementation and specification are not finalized so it is hard to say whether any compromises will have to be made when it is released. What I really want is to have some basic code contracts (like pure methods) embedded in the language in as frictionless way as possible. It eliminates need for many simple unit tests, null checks, etc. I'm not sure if this feature will be only for structs or also for classes - I hope both will work. Although I switched to C# 7 years ago from C++, and I like it, I have to say that C++ syntax seems more readable to me - const T& seems more readable to me than ref readonly T. Another thing that might be missing here is propagating immutability information down to JIT. AFAIK, codegen could optimize code better if immutability is guaranteed (at least that's part of the rationale of using const & in C++). There is PureAttribute in System.Diagnostics.Contracts, however CodeContracts seem to be mostly dead in terms of compiler and VS using information from them for static analysis. I wish this ref readonly feature could basically render all IReadOnly* interfaces useless, so that collections would have proper "ref readonly" interfaces. I don't see that in proposal - I wonder if there is a way I could contribute with that remark? I think it makes a good reason that's missing from the rationale on that github page. Lastly, I wish there was more discussion about how C++ or other languages with stronger checks do it. Pretty sure there's huge opportunity to learn on other's mistakes :). As a side note - this is what makes HN great - I can post a comment and get response from Ben Adams himself! One of the greatest .NET Core contributors who made it really fast! Thank you for that link :)
- benaadams 9y agoIs finalized, better doc might be "What's new in C# 7.2" and section "Reference semantics with value types" https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7-2 https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh... The `in` modifier on parameters, to specify that an argument is passed by reference but not modified by the called method. The `ref readonly` modifier on method returns, to indicate that a method returns its value by reference but doesn't allow writes to that object. The `readonly struct` declaration, to indicate that a struct is immutable and should be passed as an `in` parameter to its member methods. The `ref struct` declaration, to indicate that a struct type accesses managed memory directly and must always be stack allocated. ReadOnlySpan also gives immutability; though will only work over contiguous memory rather than a more general collection type; for which you'd still need to use a IReadOnly* interface either as param or generic constraint.
- polskibus 9y agoThank you for additional clarification, I can see that some of related issues like https://github.com/dotnet/csharplang/issues/38 https://github.com/dotnet/csharplang/issues/38 are not yet closed too, so it may cause additional confusion about its status. I think that a C++'s const& equivalent would be very useful in C#, especially to simplify making objects and value types immutable by default and also more performant. I created https://github.com/dotnet/csharplang/issues/1118 https://github.com/dotnet/csharplang/issues/1118 as a starter point for discussion, although I'm not sure if linking it to "ref readonly" that's available for value types is the right association to make in terms of more general C# roadmap and design.