covariance

The use of nested type parameters and recursion (C#)

旧时模样 提交于 2020-01-03 03:01:06
问题 I am trying to utilize nested parameter types which appears to be illegal. I would prefer to keep the recursive function below intact so that I do not have to duplicate logic. However my nested use of <Ttype> is making the CLR very upset (See Error listed in Code). The logic within MyMethod unavoidably creates delegates of both types Action<ClassA> and Action<ClassB> . Is there a way to alter the code below to accomplish my goal? Also what are the general restrictions concerning the use of

weighted covariance matrix in numpy

走远了吗. 提交于 2020-01-02 07:59:48
问题 I want to compute the covariance C of n measurements of p quantities, where each individual quantity measurement is given its own weight. That is, my weight array W has the same shape as my quantity array Q ( n by p ). The native np.cov() function only supports weights given to individual measurements (i.e., a vector of length n ). I can initialize a p by p matrix and iterate, but if p is large, then it's a very slow process. Since Q is known to have mean zero for each quantity (column of Q )

Using base class as generic for IEnumerable<T>

☆樱花仙子☆ 提交于 2020-01-02 05:34:09
问题 I have a good understanding of OOP in general, inheritance and polymorphism, interfaces, etc. I encountered a strange situation and I don't understand why it does not work at all... EDIT : Ok, I found out that covariance (or contravariance?) may solve this problem, but crucially we're still using .NET 2.0 How can I solve this without moving to C# 4.0 ? Here is the situation. Given these two classes : public class CustomCollectionType<T> : IEnumerable<T> { /* Implementation here, not really

MonoTouch and supporting variant generic interfaces

我的梦境 提交于 2020-01-01 09:53:22
问题 The below example compiles fine in regular Mono 2.10.9: namespace covarianttest { public interface ITest<out T> : IEnumerable<T> { } } However when I attempt compile it against MonoTouch 6.0.8 I receive this error: Error CS1961: The covariant type parameter 'T' must be invariantly valid on 'covarianttest.ITest' So am I to assume that MonoTouch doesn't support extending covariant/contravariant generic interfaces yet? If so what is the recommend workaround for this situation in MonoTouch? 回答1:

MonoTouch and supporting variant generic interfaces

こ雲淡風輕ζ 提交于 2020-01-01 09:53:12
问题 The below example compiles fine in regular Mono 2.10.9: namespace covarianttest { public interface ITest<out T> : IEnumerable<T> { } } However when I attempt compile it against MonoTouch 6.0.8 I receive this error: Error CS1961: The covariant type parameter 'T' must be invariantly valid on 'covarianttest.ITest' So am I to assume that MonoTouch doesn't support extending covariant/contravariant generic interfaces yet? If so what is the recommend workaround for this situation in MonoTouch? 回答1:

MonoTouch and supporting variant generic interfaces

我们两清 提交于 2020-01-01 09:53:09
问题 The below example compiles fine in regular Mono 2.10.9: namespace covarianttest { public interface ITest<out T> : IEnumerable<T> { } } However when I attempt compile it against MonoTouch 6.0.8 I receive this error: Error CS1961: The covariant type parameter 'T' must be invariantly valid on 'covarianttest.ITest' So am I to assume that MonoTouch doesn't support extending covariant/contravariant generic interfaces yet? If so what is the recommend workaround for this situation in MonoTouch? 回答1:

Is List<List<String>> an instance of Collection<Collection<T>>?

半腔热情 提交于 2020-01-01 09:13:18
问题 I wrote this handy, generic function for converting a collection of collections into a single set: public static <T> Set<T> makeSet(Collection<Collection<T>> a_collection) { Iterator<Collection<T>> it = a_collection.iterator(); Set<T> result = new HashSet<T>(); while (it.hasNext()) { result.addAll(it.next()); } return result; } Then I tried to call it: List<List<String>> resultLists = ... ; Set<String> labelsSet = CollectionsHelper.makeSet(resultLists); and I received the following error: <T

Is List<List<String>> an instance of Collection<Collection<T>>?

﹥>﹥吖頭↗ 提交于 2020-01-01 09:13:12
问题 I wrote this handy, generic function for converting a collection of collections into a single set: public static <T> Set<T> makeSet(Collection<Collection<T>> a_collection) { Iterator<Collection<T>> it = a_collection.iterator(); Set<T> result = new HashSet<T>(); while (it.hasNext()) { result.addAll(it.next()); } return result; } Then I tried to call it: List<List<String>> resultLists = ... ; Set<String> labelsSet = CollectionsHelper.makeSet(resultLists); and I received the following error: <T

How to determine type parameter's variance?

我是研究僧i 提交于 2020-01-01 04:28:06
问题 Inspired by Real-world examples of co- and contravariance in Scala I thought a better question would be: When designing a library, are there a specific set of questions you should ask yourself when determining whether a type parameter should be covariant or contravariant? Or should you make everything invariant and then change as needed? 回答1: Well, simple, does it make sense? Think of Liskov substitution. Co-variance If A <: B , does it make sense to pass a C[A] where a C[B] is expected? If

Type Members and Covariance

允我心安 提交于 2020-01-01 02:29:09
问题 I guess, "type variance annotations" ( + and - ) cannot be applied to "type members". In order to explain it to myself I considered the following example abstract class Box {type T; val element: T} Now if I want to create class StringBox I have to extend Box : class StringBox extends Box { type T = String; override val element = ""} So I can say that Box is naturally covariant in type T . In other words, the classes with type members are covariant in those types. Does it make sense ? How