-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Method level constraints in generic type parameter for contra and covariant returns #3702
Comments
I don't understand what you're trying to achieve or what the proposal is. Some of the things that confuse me:
|
This comment has been minimized.
This comment has been minimized.
I'm not sure I understand this either. What's the use case? In your example you take an array and return exact same array. What does that achieve? What would you expect the return type of @svick described use for variance. I don't see how does that relate to your proposal. Could you maybe provide better example?
Is this tone really necessary? |
@juliusfriedman I do not understand what you meant by that.
|
@juliusfriedman I've hidden your comment as it's not relevant to this issue, and is approaching the edge of the .NET Code of Conduct. Your ideas are getting a lot of pushback. I understand that that can be frustrating, especially since they make sense to you. Perhaps a better solution would be to join either the gitter or the csharp discord and talk through your proposals with someone. Realistically, 99% of the proposals on this repo are never going to be implemented, so anything that you can do to make your proposals more attractive and understandable to LDT members is only going to benefit them. As to this proposal, I'll join the others commenting in that I don't really understand what co/contravariance on a method type parameter would get you. There's no inheritance here, and variance only comes into play when we're talking about inheritance hierarchies. |
I don't want people to have to implement an interface with a virtual call just to run a method with already available semantics just at a method level instead of the interface level. Even if we have anonymous objects which can auto implement interfaces I would still likely want a way to control the variance of the type and I don't want to define an interface just to define the method semantics I want when I can use that already valid syntax at the method level and have it to the same thing, thus supporting multiple different return type scenarios depending on the method and type you pass rather than having an interface PER method you need. |
At this point I don't know why you spent years implementing covariant returns when it's already widely supported in generics but that doesn't seem to matter anymore as the feature is done and 📦 |
Sure I could use them along with devirtualization and have a good time.
Eh, no probably not
Yes but not the way it was done, generics supported this for a long time.
Yes we already have this, return object. |
Variance is only supported today with interfaces your parameters and delegates. We added support for covariant return types so you can also use them in classes and normal interfaces on their members. It expands the locations where this is supported (honestly to the place people have been asking about the most and for the longest). |
Can you share an example where that is necessary? Because I don't see it. Consider a trivial method with a contravariant parameter and covariant return type: interface IMap<in TInput, out TOutput>
{
TOutput Map(TInput x);
}
class Identity<T> : IMap<T, T>
{
public T Map(T x) => x;
} You can use the variant interface like this: var map = new Identity<Stream>();
IMap<MemoryStream, object> variantMap = map;
var input = new MemoryStream();
object o1 = variantMap.Map(input); If I understand you correctly, you want to treat object o2 = map.Map(input); What am I missing? |
The method If you look at the linked code there is the context. I also provided the definition of what works and what doesn't.
Co variant
You don't it seems, let me re-write pretty much what I have above....my proposal here is to allow the grammar which exists and by virtue of that fact the rules of the spec to work at any generic method level not just the generic interface.
Everything. |
That's only because of how you chose to implement it, yes I can see it being useful at the return type I guess but it was already possible via the interface definition and it's methods as @svick has shown above. All I am proposing here is to allow generic methods (which can already be defined on generic interfaces) to exist without the interface in the first place, so I can therefor have a static class which offers those methods without needing an interface and then to define my extension class on that interface. I will be a bit arrogant and say your problem I think is that you don't know how to make it work when |
Except variance is not about user-defined implicit conversions, it's pretty much only about base types and derived types. And even then, it only works on reference types, while your example is all about value types. If you're proposing to change some or all of that, you need to explicit about it. Also, that example looks like it's more about changing how type inference works (see e.g. #450 for some previous discussion about that), because, with your implicit conversions to ToArray<Number>(1, 2.0, 3.0d, 4.0m)
I don't think it's reasonable for you to assume we'll read 3000 lines of code, just to attempt to understand what probably could be explained in at most a page of text.
I don't understand what this means. |
@juliusfriedman This tone is not helping anyone -- especially not when using it against members of the language design and implementation team.
Then I still do not quite get your proposal, to be honest. (I am not trying to be an ass or anything .... I literally do not get it, even though I re-read the entire conversation 😕)
No, we dont. Contravariant returns do not exist. Disclaimer: The following segment is slightly off-topic. (@333fred do please instruct me, whether I am going against the code of conduct with this.) @juliusfriedman: I have taken the liberty to take a glance at your GitHub profile, and I saw that you have been contributing to dotnet/runtime and other .NET-related repositories (Correct me if I'm wrong, but you even have moderator privileges on said repository). I therefore do conclude that you do know what you are talking about. I also conclude that you have a somewhat in-depth knowledge of the finer workings of the .NET Runtime and/or compiler. At least you seem to have more knowledge about this subject than I have (I studied computer science at university and wrote a few niche-case compilers .... however, none of them came even close to the enormous project that is Roslyn). However, from your recent proposals I was unable to gather the core message. It might very well be that I am either not intelligent enough, nor possess enough knowledge about the Roslyn and .NET projects. Maybe, my English qualifications are not good enough (English not being my primary language). The point is that even though I consider myself to have some knowledge of compilers and the C# language in general, I simply do not get what you are requesting. I am honestly (really!) trying to understand you, but I somehow fail to get it .... which is very frustrating to me, as I want to try to help you with your proposals. Could you maybe please create a concise reply containing the following?
This could greatly help me (and probably others) in helping you with your requests! Thank you very much for reading through this. If you feel offended by anything I wrote in this (or previous) posts, I sincerely want to apologize for that. I do not bear any ill-wishes and I do not want to make ad-hominem arguments in any discussion, however I absolutely wanted to raise the points written above. |
Don't worry not offended or anything, I tried to improve it. The 1 - 2 sentences your looking for are:
Thank you for your time. And P.s. I am not a moderator I don't know where you saw that... |
Please see the example? |
@Unknown6656 you're fine. As long as we're not attacking each other here. |
May I note that I only registered constructive criticism from his post and also thank you for your concern! You are a really fair and good moderator @333fred ! |
Correct me, but contravariant returns do not exist on interfaces, either. Only contravariant parameters (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-generic-modifier). Are you proposing that contravariant parameters should also be allowed on class methods? That might be more difficult to implement, as method parameters are part of the method signature -- opposed to the return type (which explains why covariant returns were not too terribly difficult to implement).
Ah, I think I saw you attach a label to one of your own issues in the dotnet/runtime repo, and I assumed that you are a moderator, as usually only moderators can attach labels to issues or pull requests.
Thanks, mate!
Yeah, I second that. Thanks, @333fred ! |
This has happened to me when I post in some repos that I'm not a member in. I think it's GitHub automation that applies the label and attributes you as the one who triggered it. |
The user juliusfriedman was banned due to multiple COC violations on the dotnet organization. Their behavior here, and on our related Discord and Gitter communities, demonstrated a repeated pattern of personal attacks, an unwillingness to accept feedback and a constant tone of disrespectfulness to the other members of the community |
I guess that this issue can be closed then, @jaredpar ? |
Consider this valid interface declaration:
The motivation of this proposal is to simplify programming by not having to define trivial interfaces just to use in or out constraint for generic methods which should be equally easy to implement:
For example:
Usage
Additional way of implementing or in addition to with different meaning
I think that covariant and contravariant returns should be signified at the method level just as they are allowed in the interface definition for example:
Most basically:
Also
Or more crazy
Should compile? (probably not..)
Prior similar art (Math related) but I am not a math a mathematician... sorry in advance
Number
Consider
What about if I have a class:
This class is not valid (CS1960)
I can't even define static extension classes for it that have these semantic and for no good reason IMHO.
The logic routine is already in the compiler, I would be able to implement this by myself in probably less than 6 months including spec modifications with some help on just the spec writing part and maybe a point or two to some code.
The text was updated successfully, but these errors were encountered: