covariance

Why is List<T> not valid on an covariant interface MyInterface<out T>

一个人想着一个人 提交于 2019-11-29 09:50:43
Follow up question to a previous question , this has been identified as a co-variance issue. Taking this one step further, if I modify IFactory as follows: class Program { static void Main(string[] args) { IFactory<IProduct> factory = new Factory(); } } class Factory : IFactory<Product> { } class Product : IProduct { } interface IFactory<out T> where T : IProduct { List<T> MakeStuff(); } interface IProduct { } I get: Invalid variance: The type parameter T must be invariantly valid on Sandbox.IFactory.MakeStuff(). T is covariant. Why is this not invariantly valid? How can/should this be

Make dictionary read only in C#

一笑奈何 提交于 2019-11-29 09:49:45
I have a Dictionary<string, List<string>> and would like to expose the member as read only. I see that I can return it as a IReadOnlyDictionary<string, List<string>> , but I can't figure out how to return it as an IReadOnlyDictionary<string, IReadOnlyList<string>> . Is there a way to do this? In c++ I'd just be using const, but C# doesn't have that. Note that simply using a IReadOnlyDictionary does not help in this case, because I want the values to be read only as well. It appears the only way to do this is build another IReadOnlyDictionary, and add IReadOnlyList to them. Another option,

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

☆樱花仙子☆ 提交于 2019-11-29 07:35:15
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. According to one of the package vignettes (page 2), you can calculate the covariance of beta: e_i * lambda *

overriding virtual function return type differs and is not covariant

时光毁灭记忆、已成空白 提交于 2019-11-29 06:48:36
Ah, SO came back just in time. I am getting a strange error: 'B::blah': overriding virtual function return type differs and is not covariant from 'A::blah' Here is the code causing the problem: class A { public: class Inner { }; virtual Inner blah() = 0; }; class B : public A { public: class Inner2 : public Inner { }; Inner2 blah() { return Inner2(); } }; I looked up the error, and according to a page I found on the Microsoft website , one of the ways types can be covariant is if: the class in the return type of B::f is the same class as the class in the return type of D::f or, is an

Selectively disable subsumption in Scala? (correctly type List.contains)

这一生的挚爱 提交于 2019-11-29 03:01:33
List("a").contains(5) Because an Int can never be contained in a list of String , this should generate an error at compile-time , but it does not. It wastefully and silently tests every String contained in the list for equality to 5 , which can never be true ( "5" never equals 5 in Scala). This has been named " the 'contains' problem ". And some have implied that if a type system cannot correctly type such semantics, then why go through the extra effort for enforcing types. So I consider it is an important problem to solve. The type parametrization B >: A of List.contains inputs any type that

DELPHI: Generics and polymorphism

限于喜欢 提交于 2019-11-29 02:59:57
问题 This has been asked several different ways already - but I haven't found my answer yet. Can someone clarify a few things for me please. Using : Delphi XE2 I have quite a big BaseObject that I use for almost everything. Along with it I have a Generic list - BaseList. Delarations go like this : TBaseObject = class ... a lot of properties and methods ... end; TBaseList<T: TBaseObject> = class(TObjectList<T>) ... some properties and methods ... end; I have recently tried to change the TBaseList

C# variance annotation of a type parameter, constrained to be value type

房东的猫 提交于 2019-11-28 22:03:07
问题 It is possible in C# to add variance annotation to type parameter, constrained to be value type: interface IFoo<in T> where T : struct { void Boo(T x); } Why is this allowed by compiler if variance annotation makes completely no sense in a such situation? 回答1: Why this is allowed by compiler since variance annotation make completely no sense in a such situation? It's allowed by the compiler because I never even considered that someone might try to do that when I added the variance rules to

What are the kinds of covariance in C#? (Or, covariance: by example)

孤街醉人 提交于 2019-11-28 21:39:42
Covariance is (roughly) the ability to mirror inheritance of "simple" types in complex types that use them. E.g. We can always treat an instance of Cat as an instance of Animal . A ComplexType<Cat> may be treated as a ComplexType<Animal> , if ComplexType is covariant. I'm wondering: what are the "types" of covariance, and how do they relate to C# (are they supported?) Code examples would be helpful. For instance, one type is return type covariance , supported by Java, but not C#. I'm hoping someone with functional programming chops can chime in, too! Cristi Diaconescu Here's what I can think

Why can't the compiler tell the better conversion target in this overload resolution case? (covariance)

喜你入骨 提交于 2019-11-28 21:23:47
Understanding the C# Language Specification on overload resolution is clearly hard, and now I am wondering why this simple case fails: void Method(Func<string> f) { } void Method(Func<object> f) { } void Call() { Method(() => { throw new NotSupportedException(); }); } This gives compile-time error CS0121, The call is ambiguous between the following methods or properties: followed by my two Method function members (overloads). What I would have expected was that Func<string> was a better conversion target than Func<object> , and then the first overload should be used. Since .NET 4 and C# 4

Why aren't there many discussions about co- and contra-variance in Haskell (as opposed to Scala or C#)?

回眸只為那壹抹淺笑 提交于 2019-11-28 17:23:36
I know what covariance and contravariance of types are. My question is why haven't I encountered discussion of these concepts yet in my study of Haskell (as opposed to, say, Scala)? It seems there is a fundamental difference in the way Haskell views types as opposed to Scala or C#, and I'd like to articulate what that difference is. Or maybe I'm wrong and I just haven't learned enough Haskell yet :-) There are two main reasons: Haskell lacks an inherent notion of subtyping, so in general variance is less relevant. Contravariance mostly appears where mutability is involved, so most data types