covariance

Is co-variance safe here?

狂风中的少年 提交于 2019-12-24 02:20:53
问题 class Food{} class Meat extends Food{} class Animal{ void feed(Food f){} } class Lion extends Animal{ void feed(Meat m){} } void foo(Animal a){ Food f = new Food(); a.feed(f); } What will happen if we send to foo(new Lion()) ? I know that it will get error but I need the explanation please 回答1: Your Lion can eat Meat , but it can also eat any kind of food (like Spinach). If your Lion could not eat any kind of Food , then it could not be considered an implementation of Animal . This is

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:

scala properly defining a empty value for a abstract data type

你。 提交于 2019-12-23 12:06:10
问题 I have a ADT as follows: sealed trait Tree[A] case object EmptyTree extends Tree[Nothing] case class Leaf[A](value: A) extends Tree[A] case class Node[A](op: Seq[A] => A, branches: Tree[A]*) extends Tree[A] When i try to build a function to randomly create Trees i get a problem with the EmptyTree, the type system does not let go through def create(acc: Tree[A], currentDepth: Int): Tree[A] = currentDepth match { case maxDepth => Leaf(terminalSet(r.nextInt(terminalSet.length))) case 0 => { val

List, array and IEnumerable covariance

萝らか妹 提交于 2019-12-23 09:45:57
问题 I'll start with several postulates to better explain the context of my question: Array Covariance Postulate 1.1 An array of a value type is not covariant. int[] cannot pass for object[] . Postulate 1.2 An array of a reference type is covariant with a valid IEnumerable . string[] can pass for IEnumerable<object> ). Postulate 1.3 An array of a reference type is covariant with a valid covariant array. string[] can pass for object[] . List Covariance Postulate 2.1 (same as 1.1) A list of a value

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

How to conduct linear hypothesis test on regression coefficients with a clustered covariance matrix?

狂风中的少年 提交于 2019-12-23 02:05:11
问题 I am interested in calculating estimates and standard errors for linear combinations of coefficients after a linear regression in R. For example, suppose I have the regression and test: data(mtcars) library(multcomp) lm1 <- lm(mpg ~ cyl + hp, data = mtcars) summary(glht(lm1, linfct = 'cyl + hp = 0')) This will estimate the value of the sum of the coefficients on cyl and hp , and provide the standard error based on the covariance matrix produced by lm . But, suppose I want to cluster my

Plot scaled and rotated bivariate distribution using matplotlib

这一生的挚爱 提交于 2019-12-22 12:01:50
问题 I am trying to plot a bivariate gaussian distribution using matplotlib . I want to do this using the xy coordinates of two scatter points (Group A), (Group B). I want to adjust the distribution by adjusting the COV matrix to account for each Groups velocity and their distance to an additional xy coordinate used as a reference point. I've calculated the distance of each groups xy coordinate to that of the reference point. The distance is expressed as a radius , labelled [GrA_Rad] , [GrB_Rad] .

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