Replies: 19 comments
-
note that IReadonly provides covariance conversion which is useful for collections, its not useless feature, in fact some programs already took great advantage of covariance. so getting rid of IReadonly at this point is not really applicable. |
Beta Was this translation helpful? Give feedback.
-
I don't think I understand the use case that you have in mind, and why standard generic collections cannot work with covariance while being marked read only in a different way. Can you provide an example of where IReadonly<> is useful and standard interface version wouldn't work (provided that compiler gets the read only feature from a variant of readonly construct) ? |
Beta Was this translation helpful? Give feedback.
-
@voltcode any time you cast a |
Beta Was this translation helpful? Give feedback.
-
Can do |
Beta Was this translation helpful? Give feedback.
-
@benaadams does |
Beta Was this translation helpful? Give feedback.
-
@VisualMelon I'll try to describe what I mean by a C++ vector example and C++ const methods and translate of it to a possible syntax with "readonly". C++ Vector has an indexer: http://www.cplusplus.com/reference/vector/vector/operator[]/ Now to C#. IReadonlyList is basically IList without mutating methods. Translating the intention to C++ const methods, that would mean converting IReadonlyList -> const reference. If you use a const reference to IList, you are only allowed to use IList methods that are const/readonly. It seems very similar to ref readonly methods mentioned here:
a ref readonly method seems close to C++ const method in intention (albeit in my opinion less nice to read). I get that it is currently only aimed at value types at the moment, but as I mentioned before, I'd like to see this basic purity contract pushed to much greater utility. |
Beta Was this translation helpful? Give feedback.
-
@voltcode thanks for the explanation! Nonetheless, for a |
Beta Was this translation helpful? Give feedback.
-
Well, getting rid of interface dispatch might not be possible in all situations as you say, but I think the biggest take away would be to use a single "readonly" feature that can be understood from language to JIT instead of multiple IReadonly* interfaces that make immutability an opt in characteristic. As in C++, users would have to be trained to use readonly by default (tooling can help by assisting as R# hints "field can be made readonly"). New readonly behaviour would be available for all code, not just collections - anyone designing an interface or a class would be able to define which methods are readonly (in C++ const method sense) and which aren't in the same interface - no need to define a mutating one separate from the non mutating one. |
Beta Was this translation helpful? Give feedback.
-
How do you mean? (We are only using collections as examples because we all use the some ones every day!) I'm interested to hear more about how you might suggest introducing a There are obvious benefits to |
Beta Was this translation helpful? Give feedback.
-
Question - I agree that linking "const method" behavior is not well related to ireadonly collections. Should I close this issue to avoid further confusion and create new one for "const method" kind of behavior? |
Beta Was this translation helpful? Give feedback.
-
I really have no idea how this forum is meant to work - I wouldn't wish to suggest any particular course of action - but I don't suppose there would be any harm in starting a more focussed discussion with a few more 'ground assumptions' in the issue statement. I'd certainly be interested to read some more discussion on these matters. |
Beta Was this translation helpful? Give feedback.
-
No.
https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/span-safety.md more span docs: https://github.com/dotnet/corefxlab/blob/master/docs/specs/span.md (good) |
Beta Was this translation helpful? Give feedback.
-
@bbarry thanks for the clarification, and for the links (searching is not a strong point here). I'd better read them properly at some point, though I note that there is no mention of |
Beta Was this translation helpful? Give feedback.
-
I think the fact that This is certainly orthogonal to the concept in this issue of somehow using |
Beta Was this translation helpful? Give feedback.
-
I'm confused. Isn't ReadOnlySpan a ref struct? If so, then it can't implement interfaces at all. Nor would it be beneficial to do so. It can't be passed to anything that takes an interface (since you'd have to box them, and that would break the invariant that they're stack only).
Sure. But why do you need IReadOnlyList for that? It sounds like you just need an indexer, a count, and the ability foreach. -- Note: this sounds like something that would fit into Shapes (#164). Here, ReadOnlySpan and Span could fit the 'Shape' of these sorts of types, without ever having anything to do with interfaces and whatnot. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi rather: bbarry has already spoiled the surprise (and righted my previous misunderstanding).
Do not forget about runtime monomorphisation of structs (as type parameters) which implement interfaces (a feature I truly adore, though I may have the name wrong). It isn't presently clear to me why generic I do have to wonder what |
Beta Was this translation helpful? Give feedback.
-
@VisualMelon a generically constrained method can still violate stack-only as it doesn't prevent boxing in method: object DoThing<TList, T>(TList thing) where TList : IReadOnlyList<T>
{
return (object)thing;
} It would also have to be a non-boxable |
Beta Was this translation helpful? Give feedback.
-
@VisualMelon I believe the term is 'generic specialization.' |
Beta Was this translation helpful? Give feedback.
-
@benaadams indeed, it is not convertible; however, if the generic is explicitly a ref type, the compiler can know not to allow that, as you say. I would reckon on the value of generic constraints on such types being considerable (honestly, it seems an obvious feature that would warrant serious consideration, but I'm probably still missing something). @jnm2 thank you kindly, though I think that is a more general term than the one I'm looking for (I really only care about runtime specialisation over (ref) structs, which could (I reckon) allow support for projection as an interface, though as benaadams points out, the syntax is going to be a problem)! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Regarding: https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/readonly-ref.md, I'd like to add some additional "success" conditions for that specification that may be useful for extending the spec in the future.
First of all, I believe that an ideal scenario with "ref readonly" would be to be able to replace all/most IReadonly* interfaces with this language feature. It would remove the need for interface dispatch, and make users of C# less confused about the way they should move towards allowing mutability only where absolutely necessary.
Secondly, I think the proposal should also attempt to push the feature downstream, so that JIT could one day use additional knowledge about immutability to optimize program behavior. I'm not sure what's the correct process is but I believe someone more knowledgeable here could start things moving for the greater C# and .NET in general.
Next, I think a discussion about C++ (const &) and other languages that addres "ref readonly" problems would bring much to the table, at least in terms of ease of use and "default" behavior for new users.
Another related, perhaps useful story for discussion is PureAttribute from System.Diagnostics.Contracts, which fulfils similar need. Contracts in C# are in my opinion lost opportunity (meaning low adoption), partly because the tool support and usefulness for optimization were mostly not implemented. A wide "Contracts" framework like concepts in C++ could open a wide array of enhancements on all levels - from language to JIT optimization.
I realize that original ref readonly feature is related to value types, however I believe making it into a feature like const& in C++ and getting rid of IReadonly references would be beneficial for the language.
Beta Was this translation helpful? Give feedback.
All reactions