contravariance

Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work?

吃可爱长大的小学妹 提交于 2019-12-31 02:53:09
问题 Following on from this question, can someone explain the following in Scala: class Slot[+T] (var some: T) { // DOES NOT COMPILE // "COVARIANT parameter in CONTRAVARIANT position" } I understand the distinction between +T and T in the type declaration (it compiles if I use T ). But then how does one actually write a class which is covariant in its type parameter without resorting to creating the thing unparametrized ? How can I ensure that the following can only be created with an instance of

Weird example of variance rules for delegates

一曲冷凌霜 提交于 2019-12-24 03:26:39
问题 In Eric Lippert's blog posts about covariance and contravariance or variance for short, and in books such as C# in a Nutshell , it is stated that : If you’re defining a generic delegate type, it’s good practice to: Mark a type parameter used only on the return value as covariant (out). Mark any type parameters used only on parameters as contravariant (in). Doing so allows conversions to work naturally by respecting inheritance relationships between types. So I was experimenting this and I

How to fix this error? Invalid variance: The type parameter 'T' must be invariantly valid on

*爱你&永不变心* 提交于 2019-12-23 13:24:29
问题 I'm having the below error message at compile time: "Invalid variance: The type parameter 'T' must be invariantly valid on 'ConsoleApplication1.IRepository.GetAll()'. 'T' is covariant." and the below is my code: class Program { static void Main(string[] args) { IRepository<BaseClass> repository; repository = new RepositoryDerived1<Derived1>(); Console.ReadLine(); } } public abstract class BaseClass { } public class Derived1 : BaseClass { } public interface IRepository<out T> where T:

Why doesn't inheritance work the way I think it should work?

自闭症网瘾萝莉.ら 提交于 2019-12-23 07:54:18
问题 I'm having some inheritance issues as I've got a group of inter-related abstract classes that need to all be overridden together to create a client implementation. Ideally I would like to do something like the following: abstract class Animal { public Leg GetLeg() {...} } abstract class Leg { } class Dog : Animal { public override DogLeg Leg() {...} } class DogLeg : Leg { } This would allow anyone using the Dog class to automatically get DogLegs and anyone using the Animal class to get Legs.

Why doesn't inheritance work the way I think it should work?

喜欢而已 提交于 2019-12-23 07:52:54
问题 I'm having some inheritance issues as I've got a group of inter-related abstract classes that need to all be overridden together to create a client implementation. Ideally I would like to do something like the following: abstract class Animal { public Leg GetLeg() {...} } abstract class Leg { } class Dog : Animal { public override DogLeg Leg() {...} } class DogLeg : Leg { } This would allow anyone using the Dog class to automatically get DogLegs and anyone using the Animal class to get Legs.

Generic constraint for Action doesn't work as expected

孤者浪人 提交于 2019-12-23 07:39:13
问题 I am having some trouble understanding why the following snippet does not give me an error public void SomeMethod<T>(T arg) where T : MyInterface { MyInterface e = arg; } But this one, which I would expect to work due to the generic type constraint private readonly IList<Action<MyInterface>> myActionList = new List<Action<MyInterface>>(); public IDisposable Subscribe<T>(Action<T> callback) where T: MyInterface { myActionList.Add(callback); // doesn't compile return null } Gives this error

Valid type casting of both covariant and contravariant class at runtime in Scala

柔情痞子 提交于 2019-12-22 08:37:05
问题 I wrote a class implementing the command design pattern: class MyCommand[-T, +R](val name: String, val execute: T => R) , prepared two command and stored it in a MutableList: val commands = new mutable.MutableList[MyCommand[Nothing, Any]] commands += new MyCommand[String, String]("lower", s => s.toLowerCase()) commands += new MyCommand[Date, Long]("time", d => d.getTime) Then I have two data to be executed: val data = Array("StRiNG", new Date()) The problem for me is that I don't know how to

How to implement template class covariance in C++?

守給你的承諾、 提交于 2019-12-21 22:27:57
问题 Is it possible to implement a class template in such a way that one object could be casted to another if their template arguments are related? Here is an exaple to show the idea (of course it will not compile): struct Base {}; struct Derived : Base {}; template <typename T> class Foo { virtual ~Foo() {} virtual T* some_function() = 0; }; Foo<Derived>* derived = ...; Foo<Base>* base = derived; The additional problem here is that Foo is an abstract class used as an interface containing

Why are input parameters contravariant in methods?

半城伤御伤魂 提交于 2019-12-21 20:26:46
问题 Here's some code from this tutorial: case class ListNode[+T](h: T, t: ListNode[T]) { def head: T = h def tail: ListNode[T] = t def prepend(elem: T): ListNode[T] = ListNode(elem, this) } The tutorial says: Unfortunately, this program does not compile, because a covariance annotation is only possible if the type variable is used only in covariant positions. Since type variable T appears as a parameter type of method prepend, this rule is broken. How is T not in a covariant position in predend ,

Can I have a type that's both, covariant and contravariant, i.e. fully fungible/changeable with sub and super types?

做~自己de王妃 提交于 2019-12-21 13:01:13
问题 Can I have a type (for now forgetting its semantics) that can be covariant as well as contravariant? for example: public interface Foo<in out T> { void DoFooWith(T arg); } Off to Eric Lippert's blog for the meat and potatoes of variance in C# 4.0 as there's little else anywhere that covers adequate ground on the subject. I tried it out anyway, not only does it not allow that, but it tells me I am missing the whole point. I need to understand the link between read-only, write-only and variance