covariance

Why isn't DbSet covariant?

妖精的绣舞 提交于 2019-12-22 07:27:27
问题 I have a factory function to return a DbSet(Of IItemType) . The actual return type will always be an implementation IItemType , for example DbSet(Of CategoryType) . I thought covariance is supported in generics and this method would work fine, but I get an exception when I try to run my code: Unable to cast object of type 'System.Data.Entity.DbSet 1[CategoryType]' to type 'System.Data.Entity.DbSet 1[IItemType]'. 回答1: It looks like they could be covariant. But there is a host of differences

covariant type T occurs in invariant position

强颜欢笑 提交于 2019-12-22 04:08:09
问题 I'm moving my first steps in Scala and I would like to make the following code works: trait Gene[+T] { val gene: Array[T] } The error that the compiler gives is: covariant type T occurs in invariant position in type => Array[T] of value gene I know I could do something like: trait Gene[+T] { def gene[U >: T]: Array[U] } but this doesn't solve the problem because I need a value: pratically what I'm trying to say is "I don't care of the inside type, I know that genes will have a gene field that

In C# 4.0 why can't an out parameter in a method be covariant?

邮差的信 提交于 2019-12-22 01:43:24
问题 Given this magical interface: public interface IHat<out TRabbit> { TRabbit Take(); } And this class hierarchy: public class Rabbit { } public class WhiteRabbit : Rabbit { } I can now compile this: IHat<WhiteRabbit> hat1 = null; IHat<Rabbit> hat2 = hat1; Which is great. But what if I define the interface differently: public interface IHat<out TRabbit> { bool Take(out TRabbit r); } I'm indicating that the hat might be empty, using a separate boolean return value (the previous version would

How to implement template class covariance in C++?

守給你的承諾、 提交于 2019-12-21 22:27:57
问题 Is it possible to implement a class template in such a way that one object could be casted to another if their template arguments are related? Here is an exaple to show the idea (of course it will not compile): struct Base {}; struct Derived : Base {}; template <typename T> class Foo { virtual ~Foo() {} virtual T* some_function() = 0; }; Foo<Derived>* derived = ...; Foo<Base>* base = derived; The additional problem here is that Foo is an abstract class used as an interface containing

Is there a way to cast generic lists to lists of interface/base class types?

谁说我不能喝 提交于 2019-12-21 20:54:22
问题 I'm trying to show someone a use for interfaces in a crazy situation they've created. They have several unrelated objects in lists, and need to perform an operation on two string properties in each object. I'm pointing out that if they define the properties as part of an interface, they can use the interface object as the type for a method parameter that acts on it; for example: void PrintProperties(IEnumerable<ISpecialProperties> list) { foreach (var item in list) { Console.WriteLine("{0} {1

Why are input parameters contravariant in methods?

半城伤御伤魂 提交于 2019-12-21 20:26:46
问题 Here's some code from this tutorial: case class ListNode[+T](h: T, t: ListNode[T]) { def head: T = h def tail: ListNode[T] = t def prepend(elem: T): ListNode[T] = ListNode(elem, this) } The tutorial says: Unfortunately, this program does not compile, because a covariance annotation is only possible if the type variable is used only in covariant positions. Since type variable T appears as a parameter type of method prepend, this rule is broken. How is T not in a covariant position in predend ,

Can I have a type that's both, covariant and contravariant, i.e. fully fungible/changeable with sub and super types?

做~自己de王妃 提交于 2019-12-21 13:01:13
问题 Can I have a type (for now forgetting its semantics) that can be covariant as well as contravariant? for example: public interface Foo<in out T> { void DoFooWith(T arg); } Off to Eric Lippert's blog for the meat and potatoes of variance in C# 4.0 as there's little else anywhere that covers adequate ground on the subject. I tried it out anyway, not only does it not allow that, but it tells me I am missing the whole point. I need to understand the link between read-only, write-only and variance

Contravariance? Covariance? What's wrong with this generic architecture…?

久未见 提交于 2019-12-21 12:32:55
问题 I'm having some problems setting up a command handling architecture. I want to be able to create a number of different commands derived from ICommand; then, create a number of different command handlers derived from ICommandHandler; Here's the interface and classes I have begun to define: interface ICommand {} class CreateItemCommand : ICommand {} interface ICommandHandler<TCommand> where TCommand : ICommand { void Handle(TCommand command); } class CreateItemCommandHandler : ICommandHandler

Supporting both covariance and contravariance for a single type parameter [duplicate]

∥☆過路亽.° 提交于 2019-12-21 11:23:12
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Covariance and Contravariance on the same type argument You can declare a generic type parameter as covariant by using the out keyword: interface ICovariant<out R> You can declare a generic type parameter as contravariant by using the in keyword: interface IContravariant<in R> And you can also support both for different type parameters: interface IVariant<out R, in A> So why can't you suport both for a single

Python package that supports weighted covariance computation

家住魔仙堡 提交于 2019-12-21 09:31:03
问题 Is there a python statistical package that supports the computation of weighted covariance (i.e., each observation has a weight) ? Unfortuantely numpy.cov does not support weights. Preferably working under numpy/scipy framework (i.e., able to use numpy arrays to speed up the computation). Thanks a lot! 回答1: statsmodels has weighted covariance calculation in stats . But we can still calculate it also directly: # -*- coding: utf-8 -*- """descriptive statistic with case weights Author: Josef