covariance

Covariance for classes with list members in C#

折月煮酒 提交于 2019-12-12 03:24:28
问题 Consider a class hierarchy that looks as below. In essence, there is an abstract ComplexBase with some fields that are common to all classes. Then there is a ComplexClass derived from ComplexBase that, among other things, holds a collection of ComplexElement s also derived from ComplexBase . public abstract class ComplexBase { internal abstract string Identifier { get; } } public abstract class ComplexClass<T> : ComplexBase where T : ComplexElement { internal SortedList<string, T> Elements {

Generic Type parameters for covariant params array

时光怂恿深爱的人放手 提交于 2019-12-12 03:15:35
问题 I have an interface: IReadOnlyList<TEntity> Execute<TEntity>(ILambdaQuery<TEntity> query, params ILambdaQuery<TAssociatedEntity>[] associations) where TAssociatedEntity : class, IAggregateRoot; The interface is used to execute a query in a repository. Where TEntity is the type that you're expecting back and what you are querying and TAssociatedEntity are other queries that you'd like to run and that will be combined with the TEntity results that come back. Eg. var courseQuery = LambdaQuery

Set Property on a Covariant Interface

故事扮演 提交于 2019-12-11 19:53:16
问题 I have the following covariant interface: public interface IHierarchical<out T> where T : IHierarchical<T> { T Parent { get; } IEnumerable<T> Children { get; } } I then have the following interfaces which derive from the interface above: public interface ISiteMapNode<T> : ISiteMapNode where T : ISiteMapNode<T> { } public interface ISiteMapNode : IHierarchical<ISiteMapNode> { string Name { get; set; } } Finally I have the following implementation: public class SiteMapNode : ISiteMapNode

Generic collection collision

旧巷老猫 提交于 2019-12-11 19:46:03
问题 Here's my best attempt to recreate the situation. public interface IFoo { } public class Foo : IFoo { } public class Bar : IFoo { } public class CollectionOf<T> : List<IFoo> { } public class Bars : CollectionOf<Bar> { } public class Test { public void Test() { CollectionOf<IFoo> bars = new Bars(); } } Compiler complains on the instantiation. Bars is a collection of IFoos. Is this one of those covariance/contravariance issues? 回答1: The fix, if there is one would depend on what it is that's not

How to return wildcard generic?

时光毁灭记忆、已成空白 提交于 2019-12-11 16:37:41
问题 I have a type alias with parameter and I would like to return the instance of different parameter types from a method: type TC[T] = (ClassTag[T], Option[T]) def gen(x: Int): TC[_] = x match { case 0 => (classTag[Int], Option[Int](0)) case _ => (classTag[String], Option[String]("")) } This does not work and gives me error: error: type mismatch; found : (scala.reflect.ClassTag[_ >: Int with String], Option[Any]) required: TC[ ] (which expands to) (scala.reflect.ClassTag[ $1], Option[_$1])

Scala: arguments contravariant and return types are covariant why? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-11 16:33:32
问题 This question already has answers here : Isn't the argument type co- not contra-variant? (4 answers) Closed 5 years ago . In the FP in Scala course, Martin mentions, the arguments are "contravariant" while the return types are "covariant". I don't think I understood that completely - can someone help with this one? 回答1: Assume Bonobo extends Animal and you have a function foo of type Animal => Bonobo . In some other place you have a variable bar of type Bonobo => Animal . Should you be

C# 4.0 RC, Silverlight 4.0 RC Covariance

感情迁移 提交于 2019-12-11 14:29:34
问题 I am trying to develop a Silverlight 4 application using C# 4.0. I have a case like this: public class Foo<T> : IEnumerable<T> { .... } Elsewhere: public class MyBaseType : MyInterface { ... } And the usage where I am having problems: Foo<MyBaseType> aBunchOfStuff = new Foo<MyBaseType>(); Foo<MyInterface> moreGeneralStuff = myListOFStuff; Now I believe this was impossible in C# 3.0 because generic type were "Invariant". However I thought this was possible in C# 4.0 through the new covariance

C# generic type inference versus covariance - bug or restriction

浪子不回头ぞ 提交于 2019-12-11 04:54:35
问题 When a generic method with dependent parameters infers the type it gives unexpected results in some cases. If I specify the type explicitly everything works without any further Changes. IEnumerable<List<string>> someStringGroups = null; // just for demonstration IEqualityComparer<IEnumerable<string>> someSequenceComparer = null; var grouped = someStringGroups .GroupBy(x => x, someSequenceComparer); Of course, above code is not intended to be ever executed, but it demonstrates that the result

Calculate Means and Covariances for large list of dataframes, replacing loops with lapply

别说谁变了你拦得住时间么 提交于 2019-12-11 04:37:30
问题 I previously posted a question of how to create all possible combinations of a set of dataframes or the "power set" of possible data frames in this link: Creating Dataframes of all Possible Combinations without Repetition of Columns with cbind I was able to create the list of possible dataframes by first creating all possible combinations of the names of the dataframes, and storing them in Ccols , a section of which looks like this: using reduce and lapply , I then called each dataframe by

Why does IEnumerable<T> only have the “out” covariant flag and not “in” c#?

拜拜、爱过 提交于 2019-12-11 03:29:49
问题 I was wondering , why does IEnumerable<T> has ONLY the out and not the in contravariant flag ? public interface IEnumerable<out T> I can understand the out by this example : IEnumerable<string> strings = new List<string>(){"a","b","c"}; IEnumerable<object> objects = strings; object is bigger than string , so the compiler afraid that well do something like: objects = objects.First().Select(x=>5);// ( we cant insert int to string...) fine and understood. but what about if i want to use the