C# Futures: Covariant Return Types

栏目: IT技术 · 发布时间: 4年前

内容简介:A frequent API design problem is the inability to use a more specific return type when overriding a method. A good example of this is your typical Clone method.In a subclass, you may wish to implement it like this:Since FtpRequest is a subclass of Request,

A frequent API design problem is the inability to use a more specific return type when overriding a method. A good example of this is your typical Clone method.

public abstract Request Clone();

In a subclass, you may wish to implement it like this:

public override FtpRequest Clone() { ... }

Since FtpRequest is a subclass of Request, logically this makes sense. But you actually do it in .NET because overrides have to be an exact match. Nor can you have an override and a new method that only differ by the return type. So usually you end up with something complex such as:

public Request Clone() => OnClone();
protected abstract Request OnClone();

Then in the subclass:

public new FtpRequest Clone() => (FtpRequest)OnClone();
protected override Request OnClone() { ... }

The ability to change the return type of an overridden method is being explored in Proposal 49, Covariant Return Types .

When originally proposed in 2017, this feature would have been implemented using some “compiler magic”. As of October of 2019, the focus has changed towards making this a first-class feature of the CLR.

In the Covariant Return Types - Draft Specification , the IL directive .override will be changed to,

The override method must have a return type that is convertible by an identity or implicit reference conversion to the return type of the overridden base method.

Currently the rule is,

The override method and the overridden base method have the same return type.

Properties and Indexers

Properties and indexers are included in this feature, but only if they are read-only. There will not be matching support for contravariant property and index setters.

Interfaces

Methods on interfaces can override covariant methods on base interfaces using the same rules as sub-classes/base classes.

When a class implements an interface, the implementing method can be covariant with the interface method.

For purposes of interface mapping, a class member A matches an interface member B when:

A and B are methods, and the name and formal parameter lists of A and B are identical, and the return type of A is convertible to the return type of B via an identity of implicit reference conversion to the return type of B .

This rule change for implicitly implemented interfaces could result in a breaking change. This would happen in the unusual situation where a subclass re-implements an interface already implemented by the base class.

interface I1 { object M(); }
class C1 : I1 { public object M() { return "C1.M"; } }
class C2 : C1, I1 { public new string M() { return "C2.M"; } }

Andy Gocke proposed a slight modification to the rule to avoid the breaking change,

Could we change the search for mapping members to consider implicit implementations with different covariant returns iff there is no other implementation (including a default one)?

Unfortunately, this is not compatible with default implementations on interfaces. Neal Gafter writes,

I don’t see how that would work in binary compatibility scenarios. If a new version of the interface is released with a default implementation then the runtime would change to use that one instead of the implementation from the base class?

Prioritization of the necessary runtime support for covariant return types is being tracked internally at Microsoft.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

点击的奥秘:运用说服心理术提升在线影响力(全彩)

点击的奥秘:运用说服心理术提升在线影响力(全彩)

Nathalie Nahai(娜塔莉.纳海) / 陈旭 / 电子工业出版社 / 2014-9-1 / 75.00元

用户的每一次点击,不管是在虚拟商店购物,还是在浏览企业网站,或是漫无目的地把玩手机,都蕴藏着基于心理学的无穷奥秘。《点击的奥秘:运用说服心理术提升在线影响力》作者为全球知名的网络心理学家,其在《点击的奥秘:运用说服心理术提升在线影响力》中将心理学、神经科学及行为经济学巧妙地结合在一起,挖掘和提炼出一套行之有效的网络用户引导策略——既涵盖在线说服最新研究动向,也包括最前沿的科技成果,以及其他诸多惊人......一起来看看 《点击的奥秘:运用说服心理术提升在线影响力(全彩)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换