Why is Task not co-variant?

前端 未结 2 1048
逝去的感伤
逝去的感伤 2020-11-28 11:50
class ResultBase {}
class Result : ResultBase {}

Task GetResult() {
    return Task.FromResult(new Result());
}

The compiler tel

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 12:05

    According to someone who may be in the know...

    The justification is that the advantage of covariance is outweighed by the disadvantage of clutter (i.e. everyone would have to make a decision about whether to use Task or ITask in every single place in their code).

    It sounds to me like there is not a very compelling motivation either way. ITask would require a lot of new overloads, probably quite a bit under the hood (I cannot attest to how the actual base class is implemented or how special it is compared to a naive implementation) but way more in the form of these linq-like extension methods.

    Somebody else made a good point - the time would be better spent making classes covariant and contravariant. I don't know how hard that would be, but that sounds like a better use of time to me.

    On the other hand, somebody mentioned that it would be very cool to have a real yield return like feature available in an async method. I mean, without sleight of hand.

提交回复
热议问题