covariance

Storing an object that implements multiple interfaces and derives from a certain base (.net)

安稳与你 提交于 2019-12-18 06:48:37
问题 In .net, it's possible to use generics so that a function can accept arguments which support one or more interfaces and derive from a base type, even if there does not exist any single type from which all valid argument types derive. For example, one could say: Sub Foo(Of T As {IInterface1, IInterface2, SomeBaseType})(Param as T) and be allowed to pass any derivative of SomeBaseType which implements both IInterface1 and IInterface2. This will work even if SomeBaseType does not support

How are co- and contra-variance used in designing business applications?

心不动则不痛 提交于 2019-12-17 22:41:24
问题 I know about using co- and contravariance in the standard library (e.g. collections and trait Function ) I wonder how co- and contravariance are used in design of "real world" business applications. 回答1: The classic example is functions, taking the Scala interface for a function with a single argument: trait Function1[-T1, +R] Which is contravariant (the - ) for the argument, and covariant (the + ) for the return type. Why? Imagine you have these classes: class Timelord { ... } class Doctor

Generic type parameter covariance and multiple interface implementations

自作多情 提交于 2019-12-17 21:49:10
问题 If I have a generic interface with a covariant type parameter, like this: interface IGeneric<out T> { string GetName(); } And If I define this class hierarchy: class Base {} class Derived1 : Base{} class Derived2 : Base{} Then I can implement the interface twice on a single class, like this, using explicit interface implementation: class DoubleDown: IGeneric<Derived1>, IGeneric<Derived2> { string IGeneric<Derived1>.GetName() { return "Derived1"; } string IGeneric<Derived2>.GetName() { return

Simple examples of co and contravariance

淺唱寂寞╮ 提交于 2019-12-17 21:42:14
问题 Could someone provide me simple C# examples of convariance, contravariance, invariance and contra-invariance (if such thing exists). All samples I've seen so far was just casting some object into System.Object . 回答1: Could someone provide me simple C# examples of convariance, contravariance, invariance and contra-invariance (if such thing exists). I have no idea what "contra-invariance" means. The rest are easy. Here's an example of covariance: void FeedTheAnimals(IEnumerable<Animal> animals)

Generic constraint ignores co-variance

让人想犯罪 __ 提交于 2019-12-17 20:35:36
问题 Let's say we have an interface like public interface IEnumerable<out T> { /*...*/ } that is co-variant in T . Then we have another interface and a class implementing it: public interface ISomeInterface {} public class SomeClass : ISomeInterface {} Now the co-variance allows us to do the following IEnumerable<ISomeInterface> e = Enumerable.Empty<SomeClass>(); So a IEnumerable<SomeClass> is assignable to a variable (or method parameter) of type IEnumerable<ISomeInterface> . But if we try this

Invalid variance: The type parameter 'T' must be contravariantly valid on 'UserQuery.IItem<T>.ItemList'. 'T' is covariant [duplicate]

早过忘川 提交于 2019-12-17 16:35:11
问题 This question already has answers here : T must be contravariantly valid (3 answers) Closed 5 months ago . Why the property get the error while the method can be compiled? public interface IFoo {} public interface IBar<out T> where T : IFoo {} public interface IItem<out T> where T: IFoo { // IEnumerable<IBar<T>> GetList(); // works IEnumerable<IBar<T>> ItemList { get; set; } // Error! } Error: Invalid variance: The type parameter 'T' must be contravariantly valid on 'UserQuery.IItem<T>

C# casting an inherited Generic interface

有些话、适合烂在心里 提交于 2019-12-17 16:19:07
问题 I'm having some trouble getting my head around casting an interface I've come up with. It's an MVP design for C# Windows Forms. I have an IView class which I implement on my form classes. There's also an IPresenter which I derive into various specific Presenters. Each Presenter will manage the IView differently depending on the role, for example opening the dialog to enter a new set of data with an AddPresenter as opposed to editing existing data with an EditPresenter which would preload data

C# casting an inherited Generic interface

◇◆丶佛笑我妖孽 提交于 2019-12-17 16:18:16
问题 I'm having some trouble getting my head around casting an interface I've come up with. It's an MVP design for C# Windows Forms. I have an IView class which I implement on my form classes. There's also an IPresenter which I derive into various specific Presenters. Each Presenter will manage the IView differently depending on the role, for example opening the dialog to enter a new set of data with an AddPresenter as opposed to editing existing data with an EditPresenter which would preload data

Why is Function[-A1,…,+B] not about allowing any supertypes as parameters?

为君一笑 提交于 2019-12-17 15:28:01
问题 I believe one can define covariance (at least, for objects) as 'the ability to use a value of a narrower (sub) type in place of a value of some wider (super) type', and that contravariance is the exact opposite of this. Apparently, Scala functions are instances of Function[-A1,...,+B] for contravariant parameter types A1, etc. and covariant return type, B. While this is handy for subtyping on Functions, shouldn't the above definition mean I can pass any supertypes as parameters? Please advise

C++ covariance in parameters

萝らか妹 提交于 2019-12-17 11:43:47
问题 I wanted to know why C++ does not support co-variance in parameters like in example below or if there is a way to achieve it? class base { public: virtual base* func(base * ptr) { return new base(); } }; class derived : public base { public: virtual derived* func(derived * ptr) override { return new derived(); } //not allowed }; 回答1: The return type is permissible since derived inherits from base , but the function parameter can't work - not all base instances will be a derived also. What's