covariance

Generic wildcards in variable declarations in Scala

依然范特西╮ 提交于 2019-11-29 17:33:51
问题 In Java I might do this: class MyClass { private List<? extends MyInterface> list; public void setList(List<MyImpl> l) { list = l; } } ...assuming that ( MyImpl implements MyInterface ) of course. What is the analog for this in Scala , when using a Buffer ? import java.lang.reflect._ import scala.collection.mutable._ class ScalaClass { val list:Buffer[MyInterface] = null def setList(l: Buffer[MyImpl]) = { list = l } } This (of course) doesn't compile - but how do I declare the list variable

Autofac: Hiding multiple contravariant implementations behind one composite

此生再无相见时 提交于 2019-11-29 17:17:23
问题 I was triggered by this SO question about (.NET 4.0) covariance and contravariance support for Autofac, and now I'm trying to achieve something similar, but without any luck. What I am trying to achieve is configure Autofac in such way that when I resolve a single concrete IEventHandler<TEvent> (for the sake of demonstration using container.Resolve , but normally of course using constructor injection), Autofac will return me a MultipleDispatchEventHandler<TEvent> that wraps all registered

numpy covariance matrix

流过昼夜 提交于 2019-11-29 16:40:57
问题 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

What are good reasons for choosing invariance in an API like Stream.reduce()?

本秂侑毒 提交于 2019-11-29 16:35:25
问题 Reviewing Java 8 Stream API design, I was surprised by the generic invariance on the Stream.reduce() arguments: <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner) A seemingly more versatile version of the same API might have applied covariance / contravariance on individual references to U , such as: <U> U reduce(U identity, BiFunction<? super U, ? super T, ? extends U> accumulator, BiFunction<? super U, ? super U, ? extends U> combiner) This would

Overriding abstract property using more specified return type (covariance)

戏子无情 提交于 2019-11-29 15:55:37
class Base {} abstract class A { abstract public List<Base> Items { get; set; } } class Derived : Base {} class B : A { private List<Derived> items; public override List<Derived> Items { get { return items; } set { items = value; } } } The compiler says that B.Items must be List of Base elements "to match overridden member" A.Items. How can i make that work? Eugene Podskal What you've tried to accomplish initially is impossible - .NET does not support co(contra)variance for method overload . The same goes for properties, because properties are just the pair of methods . But you can make your

Covariance of Box type in Rust

旧巷老猫 提交于 2019-11-29 15:23:45
After I read the subtyping chapter of the Nomicon , I couldn't wrap my head around covariance of a type parameter. Especially for the Box<T> type, which is described as: T is covariant . However, if I write this code: trait A {} trait B: A {} struct C; impl A for C {} impl B for C {} fn foo(v: Box<A>) {} fn main() { let c = C; let b: Box<B> = Box::new(c); foo(b); } ( Playground ) error[E0308]: mismatched types --> src/main.rs:13:9 | 13 | foo(b); | ^ expected trait `A`, found trait `B` | = note: expected type `std::boxed::Box<(dyn A + 'static)>` found type `std::boxed::Box<dyn B>` B is clearly

How to make generic class that contains a Set of only its own type or subtypes as Children?

六月ゝ 毕业季﹏ 提交于 2019-11-29 14:25:37
abstract class Animal { } class Mammal : Animal { } class Dog : Mammal { } class Reptile : Animal { } class AnimalWrapper<T> where T : Animal { public ISet<AnimalWrapper<T>> Children { get; set; } } class Program { public static void Main(string[] args) { var foo = new AnimalWrapper<Mammal>(); foo.Children = new HashSet<AnimalWrapper<Mammal>>(); var child = new AnimalWrapper<Dog>(); foo.Children.Add(child); } } This obviously doesn't compile because of foo.Children.Add(child); I'm not sure if the above code is the most clear way to demonstrate what I want to do, so I will try to explain in

How to make a generic class with inheritance?

旧城冷巷雨未停 提交于 2019-11-29 12:30:50
How can I make the following code work? I don't think I quite understand C# generics. Perhaps, someone can point me in the right direction. public abstract class A { } public class B : A { } public class C : A { } public static List<C> GetCList() { return new List<C>(); } static void Main(string[] args) { List<A> listA = new List<A>(); listA.Add(new B()); listA.Add(new C()); // Compiler cannot implicitly convert List<A> listB = new List<B>(); // Compiler cannot implicitly convert List<A> listC = GetCList(); // However, copying each element is fine // It has something to do with generics (I

Storing an object that implements multiple interfaces and derives from a certain base (.net)

為{幸葍}努か 提交于 2019-11-29 11:22:50
In .net, it's possible to use generics so that a function can accept arguments which support one or more interfaces and derive from a base type, even if there does not exist any single type from which all valid argument types derive. For example, one could say: Sub Foo(Of T As {IInterface1, IInterface2, SomeBaseType})(Param as T) and be allowed to pass any derivative of SomeBaseType which implements both IInterface1 and IInterface2. This will work even if SomeBaseType does not support Interface1 and Interface2, and classes which do implement those interfaces don't share any common ancestor

How to accomplish covariant return types when returning a shared_ptr?

≯℡__Kan透↙ 提交于 2019-11-29 11:15:31
问题 using namespace boost; class A {}; class B : public A {}; class X { virtual shared_ptr<A> foo(); }; class Y : public X { virtual shared_ptr<B> foo(); }; The return types aren't covariant (nor are they, therefore, legal), but they would be if I was using raw pointers instead. What's the commonly accepted idiom to work around this, if there is one? 回答1: I think that a solution is fundamentally impossible because covariance depends on pointer arithmetic which is incompatible with smart pointers.