You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 9, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 9, 2026. It is now read-only.
This is a welcome development, and means that it would (presumably) be easier to deal with binding them; see also #216.
Unknown concern #1: what does this do with assembly versioning? If e.g. Mono.Android.dll v12 embraces covariant return types such that StringBuilder.Append() now returns StringBuilder instead of IAppendable, will that break existing code?
For most circumstances, @jonpryor can't see any issues with emitting covariant return types, mirroring what Java does. (Assuming that we can, based on the previously mentioned ABI compatibility question.)
Collection Interfaces
Once area where there will be problems is around collection interfaces. Consider Issue #647, in which generatorcurrently wants to bind DownloadDrawablesAsync.doInBackground(), a method which returns a java.util.HashMap<String, Drawable>, as a System.Collections.Generic.IDictionary<string, Drawable>. This is "friendlier" to consume, as a C# consumed, but there is no way for IDictionary<TKey, TValue> to be a subclass of Java.Util.HashMap.
It is plausible that, since we already special-case collection interfaces, they will need to continue to be special-cased.
Note that in .NET 5, the runtime has more restrictions that the C# spec.
Covariant return types are for classes only:
Only class C : B not interface I2 : I1 - C is allowed to change the return type of some method of B; I2 is not allowed to change the return type of some method in I1.
There's one restriction on return types - if B has a method Foo that returns SomeInterface, then C can't have a method SomeStruct Foo (), where SomeStruct is a valuetype that implements SomeInterface.
Not sure how this compares to the Java covariant return types, but it's something to watch out for, potentially. (Particularly the first restriction)
Unknown concern #1: what does this do with assembly versioning? If e.g. Mono.Android.dll v12 embraces covariant return types such that StringBuilder.Append() now returns StringBuilder instead of IAppendable, will that break existing code?
Re-running dotnet path/to/App.dll results in no crash; it works.
From this we can infer that changing a method override to use covariant return types is an ABI-compatible change.
(At least for this simple scenario.)
I think this might even be a source-compatible change as well, though it will cause var to change its inferred type:
vard=newDerived();varm=d.M();
m will be inferred as Base in a v1 world, and Derived in a v2 world. Offhand, I don't immediately see how this would cause currently compiling code to no longer compile, though this could change semantics:
@jpobst: with this in mind, how crazy/impossible would it be to prototype covariant return type support in generator to see if we could release for .NET 6? If there were (minor!) breakage, .NET 6 is a fair time to consider it (depending on the breakage).
I am not sure what you mean by covariant return type support in generator. Today generator has no concept of covariant return types, and generates an API that matches the Java API.
Then when csc builds the generated code the user receives a compile error informing them that covariant return types are not supported in C#. The user must then use metadata to "fix" the covariant return type to match the base method.
Thus in a .NET6 world, generator will do the exact same thing it does today. However csc will happily compile the covariant return types instead of giving an error. Profit!
For example, if we wanted to convert Mono.Android.dll to contain covariant return types, no changes would be necessary in generator. Instead we would need to delete the metadata lines that "fix" the covariant return types.
Activity
[-]C#9 Covariant Return Types?[/-][+]C#9 Covariant Return Types[/+]lambdageek commentedon Jun 11, 2020
Note that in .NET 5, the runtime has more restrictions that the C# spec.
Covariant return types are for classes only:
class C : Bnotinterface I2 : I1- C is allowed to change the return type of some method of B; I2 is not allowed to change the return type of some method in I1.Bhas a methodFoothat returnsSomeInterface, thenCcan't have a methodSomeStruct Foo (), whereSomeStructis a valuetype that implementsSomeInterface.Not sure how this compares to the Java covariant return types, but it's something to watch out for, potentially. (Particularly the first restriction)
jpobst commentedon Feb 23, 2021
Additional restrictions I found that are allowed in Java but not in C#9:
object[]->string[])List<object>->List<string>)jonpryor commentedon Mar 5, 2021
This can be supported via explicit interface implementation:
jonpryor commentedon Mar 5, 2021
This worked for me?
jonpryor commentedon Mar 5, 2021
Answer: No (?)!
At least with current .NET 5.0.103, given:
This is normal C#8 code; no covariant return types. As expected,
App.Maincontains:If we change
Derived.dllwithout rebuilding anything else:Re-running
dotnet path/to/App.dllresults in no crash; it works.From this we can infer that changing a method override to use covariant return types is an ABI-compatible change.
(At least for this simple scenario.)
I think this might even be a source-compatible change as well, though it will cause
varto change its inferred type:mwill be inferred asBasein a v1 world, andDerivedin a v2 world. Offhand, I don't immediately see how this would cause currently compiling code to no longer compile, though this could change semantics:A different
MaybeRelefvant.M()overload would be called in a v1 vs. v2 world.jonpryor commentedon Mar 5, 2021
@jpobst: with this in mind, how crazy/impossible would it be to prototype covariant return type support in
generatorto see if we could release for .NET 6? If there were (minor!) breakage, .NET 6 is a fair time to consider it (depending on the breakage).jpobst commentedon Mar 5, 2021
I am not sure what you mean by covariant return type support in
generator. Todaygeneratorhas no concept of covariant return types, and generates an API that matches the Java API.Then when
cscbuilds the generated code the user receives a compile error informing them that covariant return types are not supported in C#. The user must then use metadata to "fix" the covariant return type to match the base method.Thus in a .NET6 world,
generatorwill do the exact same thing it does today. Howevercscwill happily compile the covariant return types instead of giving an error. Profit!For example, if we wanted to convert
Mono.Android.dllto contain covariant return types, no changes would be necessary ingenerator. Instead we would need to delete the metadata lines that "fix" the covariant return types.jpobst commentedon Feb 13, 2023
Closing issue as this does not appear to require any support from JI.