-
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
Champion "Covariant Return Types" (VS 16.8, .NET 5) #49
Comments
The part about support in interfaces, does this mean something like the following? public interface IFoo { }
public interface IBar
{
IFoo Get();
}
public interface IBaz : IFoo { }
public interface IQux : IBar
{
override IBaz Get(); //or would it be 'override IBaz IBar.Get();'?
} I guess it's lucky that discussions around Default Interface Implementations have pushed back a little bit on using |
I find myself wishing C# supported covariant returns very often, and often with code that isn't particularly "clever". |
Covariant returns would be very useful for typed cloning methods, preventing the need to manually create unique protected cloning methods. |
Why didn't this happen yet? |
@MohammadHamdyGhanem
Because you haven't written it yet. Seriously, there are a million "good" ideas, modifying the language is a very expensive process and the team has finite resources. Things take time. |
If I can write "it", I would rather write a new S# (Smart Sharp) language, where I do whatever I want :). Anyway, may God help the team :) |
Would this need CLR changes, or compiler changes only? |
According to linked proposal document it is purely compiler magic. It is mentioned that compiler will spit two methods instead of one. One virtual override and another who will call this override and cast result to more specific instance so nothing fancy. Though for that reason alternative mentioned in proposal SEEMS to be better for performance but that will come at the cost of ugliness in code (method duplications). I wonder if both couldnt be combined into one proposal? So compiler will produce same code to alternative but source code will look like original proposal. |
For a long time I didn't realise that the problems I had with many of my inheritance/interface heirarchies nearly always came back to this common problem - the lack of return type covariance to allow overriding methods to narrow their return types. This then leads to duplication of methods and properties to satisfy both the base and the subclass contracts, which is a whole lot of general messyness. So I am very much in favour of this proposal. |
@gafter If you think this is likely to succeed, I will go ahead. Some pointers on the process I will need to take would be helpful. @ldematte, I assume you are no longer working on this, but if you are, please let me know, and we can discuss if I can help. Also, if you've achieved anything useful so far, it would be great if I could build on that. Also @sharwell and @HaloFour were arguing on how best to implement this, and no decision seemed to be made. Should I just go for this with Method Shadowing, or do we need to finish that discussion first? |
I think this will require some (perhaps substantial) design work before the desired/best implementation techniques are decided/agreed. |
@gafter |
@YairHalberstadt That would help. It would also help to understand how this would affect any tools that consume IL, including compilers for other languages. |
Ok. I will get started on a design proposal. |
To save you a little time, fell free to recycle the work I did when I was looking at feasibility. |
https://gist.github.com/ldematte/39b9f3119a92dc058799 Are the relevant parts |
Thanks Idematte. I'll definitely use whatever I can from your work. |
Can you be more specific? What strange/unintuitive suggestions are you referring to? Thanks! |
Honestly, I can't recall. |
I have just tried it with VS2017 and it works flawlessly, proposing them as overloads (even it is just the return type changing). |
That would be great thanks!
|
https://www.dropbox.com/sh/hw0sgrae83i93lm/AAC_YXcNxjY3q70JExCoAWfOa?dl=0 There is also a draft proposal I forgot I wrote :) |
FYI, the draft specification is at https://github.com/dotnet/csharplang/blob/master/proposals/covariant-returns.md |
My two cents. Repurpose the For example: public abstract class Foo {
public abstract object DoStuff();
}
public class ProposedBar : Foo {
public override string as object DoStuff() => "stuff"; // covariantly overrides Foo.DoStuff()
} The presence of the The use of |
@kinchungwong The compiler doesn't need any "help" to understand which method is being overridden. The return type was never part of that calculation. |
Are there any plans to make it simpler to write functions that returns
instead of (C#9)
|
There are other proposals that cover that. |
One question about Covariant Return Types Is this feature ready to be used in the last version of NET 5 (5.0.100-rc.1.20452.10 version) or is in progress yet? |
It's implemented but RC1 contains a bug with using derived type in abstract causing a TypeLoadException. Hit me. RC2 has it fixed. |
I was writing this because my Covariant Return Types code was not working as expected and I was confused about it |
I confirm you that, with the .NET 5 RC2 published yesterday, Covariant Return Types is working correctly now |
It sounds like this is now available in C# 9.0 (ref https://devblogs.microsoft.com/dotnet/c-9-0-on-the-record/#covariant-returns) so this issue can probably be closed? |
@Daniel15 such issues are closed only after spec is added (look for |
The spec leaves open the question of whether example, the spec mentions interface I1 { I1 M(); }
interface I2 { I2 M(); }
interface I3: I1, I2 { override I3 M(); } But currently |
It's a bit unclear from this thread and other notes. Is it done using "CLR hack" of just allowing covariant return types, or the compiler double-method emittance? |
The CLR was modified to allow the overriding method to have a covariant return type. I don't see why that would be considered a "hack", though. |
Perhaps they thought of the solution as a "hack" since it required users to target a new CLR as opposed to being backwards compatible with older CLR's like that of the .NET Framework. |
You'll laugh but that's how it was characterized by a member of the language team about 10 years ago when I first brought it up, before GitHub. |
Yea, things changed a lot in that time. I understand that the main reason they think differently now is the fact that changing the CLR no longer introduces the risk of automatically breaking every .NET application on your PC. |
One thing I would love, would be to write |
What is happening with doing this for interfaces as well? Is that going ahead? Is there another issue tracking that? If not, what are the arguments against doing so? I didn't see any in this thread. |
The text was updated successfully, but these errors were encountered: