covariance

.NET equivalent for Java bounded wildcard (IInterf<?>)?

流过昼夜 提交于 2019-12-04 12:17:18
问题 I'm stuck trying to translate some Java code that uses (bounded) wildcard generics to C#. My problem is, Java seems to allow a generic type to be both covariant and contravariant when used with a wildcard. [This is a spin-off from a previous question dealing with a simpler case of bounded-wildcards] Java - works: class Impl { } interface IGeneric1<T extends Impl> { void method1(IGeneric2<?> val); T method1WithParam(T val); } interface IGeneric2<T extends Impl> { void method2(IGeneric1<?> val)

MonoTouch and supporting variant generic interfaces

那年仲夏 提交于 2019-12-04 06:55:18
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? This actually depend on the compiler (and profile/runtime) not the Mono version. IOW some things might work

C#: Is a private inner interface possible?

左心房为你撑大大i 提交于 2019-12-04 06:45:24
I have a generic class X<T> ; This class has a covariant part that I want to be able to access covariantly. So I factor it out into an interface IX<out T> . However, I want this interface to be visible only to the class itself, because it contains also methods that are ment to be private . I.e., inside the class itself, I can upcast to IX<T> and use it covariantly. E.g.: class X<T> : IX<T> { private interface IX<out T>{ // the private covariant interface void foo(); } // It grants access to the private method `foo` private T foo(){...} public T IX.foo(){ return foo(); } private static void

Conversion from Func<object,string> to Func<string,string> works but to Func<int,string> fails

喜欢而已 提交于 2019-12-04 03:58:16
问题 I have the following code: static Func<object, string> s_objToString = (x) => x.ToString(); static Func<string, string> s_stringToString = s_objToString; //compiles static Func<int, string> s_intToString = s_objToString; //error The second line compiles but the third line fails to compile with error: Cannot implicitly convert type ' System.Func<object,string> ' to ' System.Func<int,string> ' Why is that? I understand that with genetics although string is derived from object a List<string>

Variance rules in C#

断了今生、忘了曾经 提交于 2019-12-04 03:09:08
The Exact rules for variance validity are a bit vague and not specific. I'm going to list the rules for what makes a type valid-covariantly, and attach some queries and personal annotations to each of those rules. A type is valid covariantly if it is: 1) a pointer type, or a non-generic type. Pointers and non-generic types are not variant in C#, except for arrays and non-generic delegates. Generic classes, structs and enums are invariant. Am I right here? 2) An array type T[] where T is valid covariantly. So this means that if the element type T of an array T[] is covariant (reference or array

Python package that supports weighted covariance computation

北战南征 提交于 2019-12-04 02:56:45
Is there a python statistical package that supports the computation of weighted covariance (i.e., each observation has a weight) ? Unfortuantely numpy.cov does not support weights. Preferably working under numpy/scipy framework (i.e., able to use numpy arrays to speed up the computation). Thanks a lot! statsmodels has weighted covariance calculation in stats . But we can still calculate it also directly: # -*- coding: utf-8 -*- """descriptive statistic with case weights Author: Josef Perktold """ import numpy as np from statsmodels.stats.weightstats import DescrStatsW np.random.seed(987467) x

Why isn't `curve_fit` able to estimate the covariance of the parameter if the parameter fits exactly?

旧城冷巷雨未停 提交于 2019-12-04 01:52:58
I don't understand curve_fit isn't able to estimate the covariance of the parameter, thus raising the OptimizeWarning below. The following MCVE explains my problem: MCVE python snippet from scipy.optimize import curve_fit func = lambda x, a: a * x popt, pcov = curve_fit(f = func, xdata = [1], ydata = [1]) print(popt, pcov) Output \python-3.4.4\lib\site-packages\scipy\optimize\minpack.py:715: OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning) [ 1.] [[ inf]] For a = 1 the function fits xdata and ydata exactly. Why isn't the error/variance 0 , or

Can/should Task<TResult> be wrapped in a C# 5.0 awaitable which is covariant in TResult?

孤街醉人 提交于 2019-12-04 01:41:52
I'm really enjoying working with C# 5.0 asynchronous programming. However, there are a few places where updating old code to be consistent with the TAP model is causing problems for me. Here's one of them - I'm not sure exactly why Task<TResult> is not covariant in TResult, but it's causing problems for me when trying to update a covariant interface to move from a synchronous to an asychronous pattern: Old code: public interface IInitializable<out T> // ** out generic modifier ** { /// <summary> /// Boolean to indicate if class is ready /// </summary> bool IsInitialized { get; } /// <summary>

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

六眼飞鱼酱① 提交于 2019-12-04 01:00:03
问题 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 this same thing using generics, the compiler would grumble at me and I would realise my stupidity at an early stage, so my question is this: Why does the C# compiler allow

Is there a way to forward declare covariance?

你。 提交于 2019-12-03 23:20:53
Suppose I have these abstract classes Foo and Bar : class Foo; class Bar; class Foo { public: virtual Bar* bar() = 0; }; class Bar { public: virtual Foo* foo() = 0; }; Suppose further that I have the derived class ConcreteFoo and ConcreteBar . I want to covariantly refine the return type of the foo() and bar() methods like this: class ConcreteFoo : public Foo { public: ConcreteBar* bar(); }; class ConcreteBar : public Bar { public: ConcreteFoo* foo(); }; This won't compile since our beloved single pass compiler does not know that ConcreteBar will inherit from Bar , and so that ConcreteBar is a