covariance

C++ covariant templates

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:23:15
问题 I feel like this one has been asked before, but I'm unable to find it on SO, nor can I find anything useful on Google. Maybe "covariant" isn't the word I'm looking for, but this concept is very similar to covariant return types on functions, so I think it's probably correct. Here's what I want to do and it gives me a compiler error: class Base; class Derived : public Base; SmartPtr<Derived> d = new Derived; SmartPtr<Base> b = d; // compiler error Assume those classes are fully fleshed out...

Why is parameter in contravariant position?

北慕城南 提交于 2019-11-26 22:15:38
问题 I'm trying to use a covariant type parameter inside a trait to construct a case-class like so: trait MyTrait[+T] { private case class MyClass(c: T) } compiler says: error: covariant type T occurs in contravariant position in type T of value c I then tried the following but it also didn't work: trait MyTrait[+T] { private case class MyClass[U <: T](c: U) } the error this time is: error: covariant type T occurs in contravariant position in type >: Nothing <: T of type U Could somebody explain

Any sensible solution to the lack of array/slice covariance in Go?

前提是你 提交于 2019-11-26 21:42:25
问题 The problem I've just faced is what to do in the following case: func printItems(header string, items []interface{}, fmtString string) { // ... } func main() { var iarr = []int{1, 2, 3} var farr = []float{1.0, 2.0, 3.0} printItems("Integer array:", iarr, "") printItems("Float array:", farr, "") } Go has no generics and doesn't allow to use collection covariance: prog.go:26: cannot use iarr (type []int) as type []interface { } in function argument prog.go:27: cannot use farr (type []float) as

C# : Is Variance (Covariance / Contravariance) another word for Polymorphism?

て烟熏妆下的殇ゞ 提交于 2019-11-26 21:30:45
I am trying to figure out the exact meaning of the words Covariance and Contravariance from several articles online and questions on StackOverflow, and from what I can understand, it's only another word for polymorphism . Am I correct with the above statement? Or have I got it wrong ? Jon Skeet It's certainly related to polymorphism. I wouldn't say they're just "another word" for polymorphism though - they're about very specific situations, where you can treat one type as if it were another type in a certain context . For instance, with normal polymorphism you can treat any reference to a

Casting List<T> - covariance/contravariance problem

本小妞迷上赌 提交于 2019-11-26 21:04:55
Given the following types: public interface IMyClass { } public class MyClass : IMyClass { } I wonder how can I convert a List<MyClass> to a List<IMyClass> ? I am not completely clear on the covariance/contravariance topics, but I understand that I cannot just plainly cast the List because of that. I could come up with this trivial solution only; lacking any elegance, wasting resources: ... public List<IMyClass> ConvertItems(List<MyClass> input) { var result = new List<IMyClass>(input.Count); foreach (var item in input) { result.Add(item); } return result; } .... How can you solve it in a more

Why cannot IEnumerable<struct> be cast as IEnumerable<object>?

安稳与你 提交于 2019-11-26 20:59:55
Why is the last line not allowed? IEnumerable<double> doubleenumerable = new List<double> { 1, 2 }; IEnumerable<string> stringenumerable = new List<string> { "a", "b" }; IEnumerable<object> objects1 = stringenumerable; // OK IEnumerable<object> objects2 = doubleenumerable; // Not allowed Is this because double is a value type that doesn't derive from object, hence the covariance doesn't work? Does that mean that there is no way to make this work: public interface IMyInterface<out T> { string Method(); } public class MyClass<U> : IMyInterface<U> { public string Method() { return "test"; } }

Why can't I cast a dictionary of one value type to dictionary of another value type when the value types can be cast from one another? [duplicate]

有些话、适合烂在心里 提交于 2019-11-26 20:58:50
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: In C#, why can't a List<string> object be stored in a List<object> variable Why doesn't the below work? List<string> castMe = new List<string>(); IEnumerable<string> getFromCast = (IEnumerable<string>)castMe; // allowed. Dictionary<int, List<string>> castMeDict = new Dictionary<int, List<string>>(); Dictionary<int, IEnumerable<string>> getFromDict = (Dictionary<int, IEnumerable<string>>)castMeDict; // Not

Container covariance in C++

血红的双手。 提交于 2019-11-26 20:53:42
问题 I know that C++ doesn't support covariance for containers elements, as in Java or C#. So the following code probably is undefined behavior: #include <vector> struct A {}; struct B : A {}; std::vector<B*> test; std::vector<A*>* foo = reinterpret_cast<std::vector<A*>*>(&test); Not surprisingly, I received downvotes when suggesting this a solution to another question. But what part of the C++ standard exactly tells me that this will result in undefined behavior? It's guaranteed that both std:

Why classes that implement variant interfaces remain invariant?

空扰寡人 提交于 2019-11-26 20:41:57
问题 C# 4.0 has extended the co and contravariance further for generic types and interfaces. Some interfaces (like IEnumerable<T> ) are covariants, so I can do things like: IEnumerable<object> ie = new List<string>(); but what about this line? I got a compile-time error List<Object> list = new List<String>(); //Cannot implicitly convert type List<string>' to List<object>' I mean, if List<T> implement IEnumerable<T> why List<T> is still invariant? Is out there a good counterexample that explain why

Why can't I assign a List<Derived> to a List<Base>?

落花浮王杯 提交于 2019-11-26 19:01:10
I defined the following class: public abstract class AbstractPackageCall { ... } I also define a subclass of this class: class PackageCall : AbstractPackageCall { ... } There are also several other subclases of AbstractPackageCall Now I want to make the following call: List<AbstractPackageCall> calls = package.getCalls(); But I always get this exception: Error 13 Cannot implicitly convert type 'System.Collections.Generic.List<Prototype_Concept_2.model.PackageCall>' to 'System.Collections.Generic.List<Prototype_Concept_2.model.AbstractPackageCall>' What is the problem here? This is the method