The Wayback Machine - https://web.archive.org/web/20210122105457/https://github.com/xamarin/java.interop/issues/649
Skip to content
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

C#9 Covariant Return Types #649

Open
jonpryor opened this issue May 19, 2020 · 1 comment
Open

C#9 Covariant Return Types #649

jonpryor opened this issue May 19, 2020 · 1 comment

Comments

@jonpryor
Copy link
Member

@jonpryor jonpryor commented May 19, 2020

C#9 will reportedly support covariant return types, which in many ways mirrors Java covariant return types.

Covariant return types is a feature in which a subclass of a type can be specified in a method override in a derived type; consider Java:

public interface Appendable {
    Appendable append(char c);
}

public class StringBuilder implements Appendable {
    StringBuilder append(char c) {…}
}

Because StringBuilder ISA Appendable, StringBuilder can be used as the Appendable.append(char) return type instead of using Appendable.

Compare to the existing Xamarin.Android Java.Lang.StringBuilder.Append(char) binding, in which IAppendable must be returned, because C# lacks covariant return types.

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 generator currently 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.

@jpobst jpobst changed the title C#9 Covariant Return Types? C#9 Covariant Return Types May 19, 2020
@lambdageek
Copy link
Member

@lambdageek lambdageek commented Jun 11, 2020

Note that in .NET 5, the runtime has more restrictions that the C# spec.

Covariant return types are for classes only:

  1. 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.
  2. 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants