covariance

Why can't I assign List<int> to IEnumerable<object> in .NET 4.0

元气小坏坏 提交于 2019-12-28 20:37:32
问题 I try to do this: IEnumerable<object> ids = new List<string>() { "0001", "0002", "0003" }; it works great! But when I try to do this: IEnumerable<object> intIds = new List<System.Int32>() { 1, 2, 3 }; Visual Studio tells me: Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) Why is that? 回答1: int is a value type and can only be boxed to object - it doesn't inherit from object .

Why is List<Number> not a sub-type of List<Object>?

允我心安 提交于 2019-12-28 17:41:15
问题 public void wahey(List<Object> list) {} wahey(new LinkedList<Number>()); The call to the method will not type-check. I can't even cast the parameter as follows: wahey((List<Object>) new LinkedList<Number>()); From my research, I have gathered that the reason for not allowing this is type-safety. If we were allowed to do the above, then we could have the following: List<Double> ld; wahey(ld); Inside the method wahey, we could add some Strings to the input list (as the parameter maintains a

Compute covariance matrix from list of occurrences [duplicate]

主宰稳场 提交于 2019-12-25 17:14:11
问题 This question already has answers here : How do I calculate the co-occurrence in the table? (3 answers) Closed 2 years ago . I have the following data frame: # my_data id cg 1 a 2 b 3 a 3 b 4 b 4 c 5 b 5 c 5 d 6 d I would like to compute the covariance of the values of cg . I believe I can obtain it by using cov() on the following matrix, where every cell counts the number of co-occurrences between two values of cg . # my_matrix cg a b c d a 2 1 0 0 b 1 4 2 1 c 0 2 2 1 d 0 1 1 2 What is the

What violations does using a covariant type in a contravariant position here enable?

大憨熊 提交于 2019-12-25 08:48:13
问题 Consider an interface with a covariant type T . I'm examining the case where properties in all derived classes of this interface that use T are readonly, and covariant if a generic class. Suppose this interface then defines a method that uses T as an argument type. What violations does it permit? For example, consider: interface ICov<out T> { void maybe_safe_set(T v); } class ImplCov<T> : ICov<T> { public readonly T a; public readonly IEnumerable<T> b; public readonly IEnumerable<IEnumerable

When “sharing” generics across traits, contravariant error for parameters of that type

淺唱寂寞╮ 提交于 2019-12-25 06:35:02
问题 I want to share the type defined in class Base with class SometimesUsedWithBase trait Base[A] { type BaseType = A } trait SometimesUsedWithBase { this: Base[_] => def someFunction(in: BaseType): BaseType } class StringThing extends Base[String] with SometimesUsedWithBase { def someFunction(in: String): String = "" + in } This solution worked fine until I added the parameter to someFunction of type BaseType. (so if you remove the parameter, the code works fine). Now I get this error: Error:(7,

c++ Covariance issue with pointer return issue

ε祈祈猫儿з 提交于 2019-12-24 21:07:41
问题 Since I had a lot of ambiguity in my post, I will redo it. This is a problem I am encountering in a project which I am upgrading from a visual studio 6.0 environment to a visual studio 2012 environment. I have a class which is derived from the followinh mfc class (CPropertyPage) which contains the following function. file is afxdlgs.h (mfc class) class CPropertyPage : public CDialog { public: virtual CPropertySheet *GetParentSheet(); } I also still seem to have the problem after changing the

Put a struct as a generic covariant parameter

狂风中的少年 提交于 2019-12-24 20:18:01
问题 I've an Covariant interface, which is in an array of it's parent type. They are defined globally like this: //The interface/class public interface IMyType<out T>{} public class MyType<T>: IMyType<T>{ T value; MyType<T>(T initialValue){ value = initialValue; } } I tried this: IMyType< object>[] tt = new IMyType<object>[] { new MyType<String>( "test"), //Works fine new MyType<SomeOtherRandomClass>(new SomeOtherRandomClass()),//works fine new MyType<Int32>(12)//Doesn't work }; Even trying to

how to compute covariance in tensorflow?

雨燕双飞 提交于 2019-12-24 10:33:17
问题 I have a problem that I don't know how to compute the covariance of two tensor. I have tried the contrib.metrics.streaming_covariance . But is always returns 0 . There must be some errors. 回答1: You could use the definition of the covariance of two random variables X and Y with the expected values x0 and y0 : cov_xx = 1 / (N-1) * Sum_i ((x_i - x0)^2) cov_yy = 1 / (N-1) * Sum_i ((y_i - y0)^2) cov_xy = 1 / (N-1) * Sum_i ((x_i - x0) * (y_i - y0)) The crucial point is to estimate x0 and y0 here,

Subclasses and return types

怎甘沉沦 提交于 2019-12-24 08:43:21
问题 let's say I wanted to have something like the following: abstract class PDF[T, S <: PDF[T, _]] { def fit(obs: Seq[T], weights: Seq[Double]): S } class PDFGaussian(val mu: Double, val Sigma: Double) extends PDF[Double, PDFGaussian] { def fit(obs: Seq[Double], weights: Seq[Double]): PDFGaussian = new PDFGaussian(...) // bla bla bla } So, basically, what I want is to have the fit function to return an instance of the type of its enclosing class which obviously must be a subclass of PDF[T] .

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