covariance

Covariance in Java

Why does the following not work in Java? It would work in C#: public static final List<String> Split(String str, char delimiter) { if ((str == null) || "".equals(str)) { return new CopyOnWriteArrayList<String>(); } } I get an error saying this method has to return List. CopyOnWriteArrayLis...

Why won't this cast work?

I have the following code: var commitmentItems = new List<CommitmentItem<ITransaction>>(); commitmentItems.Add(new CapitalCallCommitmentItem()); And I get the following error: Argument '1': cannot convert from 'Models.CapitalCallCommitmentItem' to 'Models.CommitmentItem<Models.ITransaction>' However, CapitalCallCommitmentItem ...

could someone explain the connection between type covariance/contravariance and category theory?

I am just starting to read about category theory, and would very much appreciate it if someone could explain the connection between CS contravariance/covariance and category theory. What would some example categories be (i.e. what are their objects/morphisms?)? Thanks in advance? ...

Using matlab to calculate the properties of a polygon defined as a list of points

Does MATLAB have a built-in function to find general properties like center of mass & moments of inertia for a polygon defined as a list of (non-integer valued) points? regionprops performs this task for integer valued points, on the assumption that these represent indices of pixels in an image. But the only functions I can find that...

covariance matrix in opencv

Hi everyone, Here is my code to calculate covariance matrix over a particular area of color image,But it always gives all zeroes.Please help me where am going wrong. IplImage* img = cvLoadImage("image.png",1); cvShowImage("input",img); cvSetImageROI(img,cvRect(10,10,30,30)); { CvMat* covarMatrix=cvCreateMat(3,3,CV_32FC1)...

Conversion of IEnumerable<T> for Extension Method Issue

I have the following class and extension class (for this example): public class Person<T> { public T Value { get; set; } } public static class PersonExt { public static void Process<TResult>(this Person<IEnumerable<TResult>> p) { // Do something with .Any(). Console.WriteLine(p.Value.Any()); } } I was ...

How is IEnumerable<T> Contra-variant ?

This post (http://blogs.msdn.com/b/brada/archive/2005/01/18/355755.aspx) says IEnumerable<T> is Contra-variant. However type T is co-variant because it is an out parameter. So in what context is IEnumerable<T> Contra-variant ?? Hope I am not confusing! Thanks for the answers in advance! ...

Why does MATLAB native function cov (covariance matrix computation) use a different divisor than I expect?

Given a data matrix data of M dimensions and N samples, say, data = randn(N, M); I could compute the covariance matrix with data_mu = data - ones(N, 1)*mean(data); cov_matrix = (data_mu'*data_mu)./N If I use the native MATLAB function cov_matrix2 = cov(data) this will always be equal to cov_matrix = (data_mu'*data_mu)./(N-1) ...

Question about combating contravariance. Callback-related question.

Hi guys, I have the following piece of code which doesn't compile when I try to instance something like CommandGlobal<int> because it tries to override virtual void Execute() const =0; with a function which returns int. It gives a non-covariance error. class CommandBase { public: virtual void Execute() const =0; }; template<class...

Contra- and Co-variance - CLR via C#

In the CLR via c# third edition there is an example that I cant seem to make sense of: Invariant Meaning that that generic type parameter cannot be changed. I have shown only invariant generic type parameters so far in this chapter. n Contravariant Meaning that the generic type parameter can change from a class to a cl...

A design problem involving Collections, Generics and Interfaces in C#

This post contains a lot of code, but I would really appreciate it if you took the some to read and understand it...and hopefully come up with a solution Let's assume that I am structuring a network-game where the entities need to be drawn and some of them updated from the server accordingly. The Drawable class is in charge of drawing ...

Co- and contravariant types in generic priority queue

I'm trying to implement in Scala a generic data type parameterized on a type T, which should be Ordered[T]. Specifically, it's a persistent version of Sleator & Tarjan's skew heap priority queues. After adding lots of complicated type parameter declarations based on the explanation here and in Odersky-Spoon-Venners, I'm down to one compi...

Why is C# 4.0's covariance/contravariance limited to parameterized interface and delegate types?

Is this a limitation of the CLR or are there compatibility concerns with existing code? Is this related to the messed up variance of delegate combining in C# 4.0? Edit: Would it be possible to have a language using co-/contravariance without that limitation running on the CLR? ...

still confused about covariance and contravariance & in/out

ok i read a bit on this topic on stackoverflow, watched this & this, but still a bit confused about co/contra-variance. from here Covariance allows a "bigger" (less specific) type to be substituted in an API where the original type is only used in an "output" position (e.g. as a return value). Contravariance allows a "sm...

Why are C# arrays covariant and what benefits does it bring?

Hi all, I'm having trouble understanding why arrays in C# are covariant and what benefits this covariance can bring. Consider the following trivial code example: object[] myArray = new string[1]; myArray[0] = 1; This code will compile okay, but will unceremoniously and perhaps unsurprisingly explode at runtime. If I try to attempt t...

How do I convert from List<?> to List<T> in Java using generics?

In Java, how do I convert List<?> to List<T> using a general purpose method so that I can replace patterns like the following with a single method call: List untypedList = new ArrayList(); // or returned from a legacy method List<Integer> typedList = new ArrayList<Integer>(); for (Object item: untypedList) typedList.add((Integer)it...

Override a Property with a Derived Type and Same Name C#

I'm trying to override a property in a base class with a different, but derived type with the same name. I think its possible by covarience or generics but am not sure how to do it? The following code gets the error: Error 1 'Sun.Cache': type must be 'OuterSpace.Cache' to match overridden member 'OuterSpace.Cache' public class...

Examples of good, real-life use-cases for covariance and contravariance in C# 4.0?

Before C# 4.0 came out, I was quite excited about covariance and contravariance. It pandered to my fondness for theoretical correctness! However, now that it’s out, and I’m back to writing normal, everyday, boring code, I’m starting to wonder: did I ever use it yet? And I notice that I haven’t used it consciously. None of the interfaces...

Mnemonic for C# generic types

I often forget if i have to use in or out when defining covarient and contravarient generic types. In java i have the mnemonic PECS (producer extends consumer super) to help me. Do you know a similar mnemonic for c#? ...

Why generic interfaces are not co/contravariant by default?

For example IEnumerable<T> interface: public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); } In this interface the generic type is used only as a return type of interface method and not used as a type of method arguments thus it can be covariant. Giving this, can't compiler theoretically infer the va...