covariance

Is C# 4.0 Tuple covariant

烈酒焚心 提交于 2019-11-30 18:47:01
(I would check this out for myself, but I don't have VS2010 (yet)) Say I have 2 base interfaces: IBaseModelInterface IBaseViewInterface And 2 interfaces realizing those: ISubModelInterface : IBaseModelInterface ISubViewInterface : IBaseViewInterface If I define a Tuple<IBaseModelInterface, IBaseViewInterface> I would like to set that based on the result of a factory that returns Tuple<ISubModelInterface, ISubViewInterface> . In C# 3 I can't do this even though the sub interfaces realize the base interfaces. And I'm pretty sure C# 4 lets me do this if I was using IEnumerable<IBaseModelInterface

F# and interface covariance: what to do? (specifically seq<> aka IEnumerable<>)

青春壹個敷衍的年華 提交于 2019-11-30 17:51:11
I'm trying to call a .NET method accepting a generic IEnumerable<T> from F# using a seq<U> such that U is a subclass of T. This doesn't work the way I expected it would: With the following simple printer: let printEm (os: seq<obj>) = for o in os do o.ToString() |> printfn "%s" These are the results I get: Seq.singleton "Hello World" |> printEm // error FS0001; //Expected seq<string> -> 'a but given seq<string> -> unit Seq.singleton "Hello World" :> seq<obj> |> printEm // error FS0193; //seq<string> incompatible with seq<obj> Seq.singleton "Hello World" :?> seq<obj> |> printEm // works! Seq

Why method defined like “cons[B >: A](v: B)” accepts argument of type which is not supertype of A?

人盡茶涼 提交于 2019-11-30 15:41:51
问题 I am studying variance in scala right now, and I think I have a good understanding of contravariance. For example given trait List[-A] , I know that List[Int] is a supertype of List[AnyVal] . But say that I have the following trait: trait List[+A] { def cons(hd: A): List[A] } Why is cons parameter type wrong? Why it is necessary to have def cons[B >: A](v: B): List[B] ? For example: val animal_list: List[Animal] = List(tiger, dog) if we call: animal_list.cons(tiger) since Tiger <: Animal ,

Why method defined like “cons[B >: A](v: B)” accepts argument of type which is not supertype of A?

放肆的年华 提交于 2019-11-30 15:00:13
I am studying variance in scala right now, and I think I have a good understanding of contravariance. For example given trait List[-A] , I know that List[Int] is a supertype of List[AnyVal] . But say that I have the following trait: trait List[+A] { def cons(hd: A): List[A] } Why is cons parameter type wrong? Why it is necessary to have def cons[B >: A](v: B): List[B] ? For example: val animal_list: List[Animal] = List(tiger, dog) if we call: animal_list.cons(tiger) since Tiger <: Animal , doesn't cons ran into problem? Since B is Tiger and A is Animal and B >: A is not true. TeWu Why is cons

Faster way to cast a Func<T, T2> to Func<T, object>? [duplicate]

懵懂的女人 提交于 2019-11-30 14:38:48
问题 This question already has answers here : How to cast Expression<Func<T, DateTime>> to Expression<Func<T, object>> (4 answers) Closed 6 years ago . Is there a faster way to cast Fun<TEntity, TId> to Func<TEntity, object> public static class StaticAccessors<TEntity> { public static Func<TEntity, TId> TypedGetPropertyFn<TId>(PropertyInfo pi) { var mi = pi.GetGetMethod(); return (Func<TEntity, TId>)Delegate.CreateDelegate(typeof(Func<TEntity, TId>), mi); } public static Func<TEntity, object>

Delegate Covariance Confusion Conundrum!

邮差的信 提交于 2019-11-30 13:52:38
Why does this not work? Do I not understand delegate covariance correctly? public delegate void MyDelegate(object obj) public class MyClass { public MyClass() { //Error: Expected method with 'void MyDelegate(object)' signature _delegate = MyMethod; } private MyDelegate _delegate; public void MyMethod(SomeObject obj) {} } Correct - you don't understand covariance correctly - yet :) Your code would work if you had the same types but as return values, like this: public delegate object MyDelegate() public class MyClass { public MyClass() { _delegate = MyMethod; } private MyDelegate _delegate;

C++: How can I avoid “invalid covariant return type” in inherited classes without casting?

浪子不回头ぞ 提交于 2019-11-30 13:14:22
问题 I have a quite complex class hierarchy in which the classes are cross-like depending on each other: There are two abstract classes A and C containing a method that returns an instance of C and A, respectively. In their inherited classes I want to use a co-variant type, which is in this case a problem since I don't know a way to forward-declare the inheritance relation ship. I obtain a "test.cpp:22: error: invalid covariant return type for ‘virtual D* B::outC()’"-error since the compiler does

numpy covariance matrix

你。 提交于 2019-11-30 11:08:07
Suppose I have two vectors of length 25, and I want to compute their covariance matrix. I try doing this with numpy.cov, but always end up with a 2x2 matrix. >>> import numpy as np >>> x=np.random.normal(size=25) >>> y=np.random.normal(size=25) >>> np.cov(x,y) array([[ 0.77568388, 0.15568432], [ 0.15568432, 0.73839014]]) Using the rowvar flag doesn't help either - I get exactly the same result. >>> np.cov(x,y,rowvar=0) array([[ 0.77568388, 0.15568432], [ 0.15568432, 0.73839014]]) How can I get the 25x25 covariance matrix? You have two vectors, not 25. The computer I'm on doesn't have python so

How is covariance cooler than polymorphism…and not redundant?

隐身守侯 提交于 2019-11-30 09:19:28
.NET 4 introduces covariance. I guess it is useful. After all, MS went through all the trouble of adding it to the C# language. But, why is Covariance more useful than good old polymorphism? I wrote this example to understand why I should implement Covariance, but I still don't get it. Please enlighten me. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sample { class Demo { public delegate void ContraAction<in T>(T a); public interface IContainer<out T> { T GetItem(); void Do(ContraAction<T> action); } public class Container<T> : IContainer<T> {

How to get covariance matrix for random effects (BLUPs/conditional modes) from lme4

試著忘記壹切 提交于 2019-11-30 08:42:42
问题 So, I've fitted a linear mixed model with two random intercepts in R: Y = X beta + Z b + e_i, where b ~ MVN (0, Sigma) ; X and Z are the fixed- and random-effects model matrices respectively, and beta and b are the fixed-effect parameters and random-effects BLUPs/conditional modes. I would like to get my hands on the underlying covariance matrix of b , which doesn't seem to be a trivial thing in lme4 package. You can get only the variances by VarCorr , not the actual correlation matrix.