covariance

Can I Override with derived types?

落爺英雄遲暮 提交于 2019-11-26 15:40:12
问题 As far as i know it is not possible to do the following in C# 2.0 public class Father { public virtual Father SomePropertyName { get { return this; } } } public class Child : Father { public override Child SomePropertyName { get { return this; } } } I workaround the problem by creating the property in the derived class as "new", but of course that is not polymorphic. public new Child SomePropertyName Is there any solution in 2.0? What about any features in 3.5 that address this matter? 回答1:

scala - Any vs underscore in generics

Deadly 提交于 2019-11-26 15:03:55
问题 What is the different between the following Generics definitions in Scala: class Foo[T <: List[_]] and class Bar[T <: List[Any]] My gut tells me they are about the same but that the latter is more explicit. I am finding cases where the former compiles but the latter doesn't, but can't put my finger on the exact difference. Thanks! Edit: Can I throw another into the mix? class Baz[T <: List[_ <: Any]] 回答1: OK, I figured I should have my take on it, instead of just posting comments. Sorry, this

Java covariance

时间秒杀一切 提交于 2019-11-26 14:37:40
问题 I'm having a hard time trying to figure this out. Say I have the following code: class Animal { } class Mammal extends Animal { } class Giraffe extends Mammal { } ... public static List<? extends Mammal> getMammals() { return ...; } ... public static void main(String[] args) { List<Mammal> mammals = getMammals(); // compilation error } Why does the assignment result in a compilation error? The error is something like: Type mismatch: cannot convert from List<capture#4-of ? extends Mammal> to

Why are contravariant parameter types in Java not allowed for overriding?

谁说胖子不能爱 提交于 2019-11-26 14:22:01
问题 When overriding a method of a superclass, Java allows the return type to be covariant. Why are contravariant parameter types in contrast not allowed when overriding methods? 回答1: Because that's called overloading. In particular, the return type type can be covariant because it is not considered when overloading, and it therefore still matches the superclass or interface's implementation. Parameters are considered when overloading. You very well might have an optimization with Number

C#: Overriding return types

╄→гoц情女王★ 提交于 2019-11-26 14:06:55
Is there way to override return types in C#? If so how, and if not why and what is a recommended way of doing it? My case is that I have an interface with an abstract base class and descendants of that. I would like to do this (ok not really, but as an example!) : public interface Animal { Poo Excrement { get; } } public class AnimalBase { public virtual Poo Excrement { get { return new Poo(); } } } public class Dog { // No override, just return normal poo like normal animal } public class Cat { public override RadioactivePoo Excrement { get { return new RadioActivePoo(); } } } RadioactivePoo

T must be contravariantly valid

白昼怎懂夜的黑 提交于 2019-11-26 14:05:54
问题 What is wrong with this? interface IRepository<out T> where T : IBusinessEntity { IQueryable<T> GetAll(); void Save(T t); void Delete(T t); } It says: Invalid variance: The type parameter 'T' must be contravariantly valid on 'MyNamespace.IRepository.Delete(T)'. 'T' is covariant. 回答1: Consider what would happen if the compiler allowed that: interface IR<out T> { void D(T t); } class C : IR<Mammal> { public void D(Mammal m) { m.GrowHair(); } } ... IR<Animal> x = new C(); // legal because T is

What is a covariant return type?

本秂侑毒 提交于 2019-11-26 14:02:48
What is a covariant return type in Java? In object-oriented programming in general? Andrzej Doyle Covariant return, means that when one overrides a method, the return type of the overriding method is allowed to be a subtype of the overridden method's return type. To clarify this with an example, a common case is Object.clone() - which is declared to return a type of Object . You could override this in your own class as follows: public class MyFoo { ... // Note covariant return here, method does not just return Object public MyFoo clone() { // Implementation } } The benefit here is that any

IDictionary<TKey, TValue> in .NET 4 not covariant

早过忘川 提交于 2019-11-26 13:22:35
The IDictionary<TKey, TValue> in .NET 4 / Silverlight 4 does not support covariance, i.e. I can't do a IDictionary<string, object> myDict = new Dictionary<string, string>(); analog to what I can do with IEnumerable<T> s now. Probably boils down to the KeyValuePair<TKey, TValue> not being covariant either. I feel that covariance should be allowed in dictionaries at least for the values. So is that a bug or a feature? Will it ever come, maybe in .NET 37.4? UPDATE (2 years later): There will be an IReadOnlyDictionary<TKey, TValue> in .NET 4.5, but it won't be covariant either :·/ , because it

<out T> vs <T> in Generics

懵懂的女人 提交于 2019-11-26 12:41:28
What is the difference between <out T> and <T> ? For example: public interface IExample<out T> { ... } vs. public interface IExample<T> { ... } Reed Copsey The out keyword in generics is used to denote that the type T in the interface is covariant. See Covariance and contravariance for details. The classic example is IEnumerable<out T> . Since IEnumerable<out T> is covariant, you're allowed to do the following: IEnumerable<string> strings = new List<string>(); IEnumerable<object> objects = strings; The second line above would fail if this wasn't covariant, even though logically it should work,

Why are Arrays invariant, but Lists covariant?

早过忘川 提交于 2019-11-26 12:39:42
问题 E.g. why does val list:List[Any] = List[Int](1,2,3) work, but val arr:Array[Any] = Array[Int](1,2,3) fails (because arrays are invariant). What is the desired effect behind this design decision? 回答1: Because it would break type-safety otherwise. If not, you would be able to do something like this: val arr:Array[Int] = Array[Int](1,2,3) val arr2:Array[Any] = arr arr2(0) = 2.54 and the compiler can't catch it. On the other hand, lists are immutable, so you can't add something that is not Int