covariance

Contravariant method argument type

邮差的信 提交于 2019-12-05 15:55:32
wiki Contravariant_method_argument_type says overriding method has the subtyping rule as function type, but no language except one support contravariant argument type. I also not able to come up with any idea of benefit to use that. example: class AnimalShelter { Animal getAnimalForAdoption() { ... } void putAnimal(Animal animal) { ... } } class CatShelter extends AnimalShelter { @Overriding Cat getAnimalForAdoption() { return new Cat(); } @Overriding void putAnimal(Object animal) { … } } My question is: Is contravariant argument type of overriding method any of good use? if yes, where it is?

Question about C# 4.0's generics covariance

[亡魂溺海] 提交于 2019-12-05 12:59:12
问题 Having defined this interface: public interface IInputBoxService<out T> { bool ShowDialog(); T Result { get; } } Why does the following code work: public class StringInputBoxService : IInputBoxService<string> { ... } ... IInputBoxService<object> service = new StringInputBoxService(); and this doesn't?: public class IntegerInputBoxService : IInputBoxService<int> { ... } ... IInputBoxService<object> service = new IntegerInputBoxService(); Does it have anything to do with int being a value type?

Is there a way to forward declare covariance?

浪尽此生 提交于 2019-12-05 11:37:50
问题 Suppose I have these abstract classes Foo and Bar : class Foo; class Bar; class Foo { public: virtual Bar* bar() = 0; }; class Bar { public: virtual Foo* foo() = 0; }; Suppose further that I have the derived class ConcreteFoo and ConcreteBar . I want to covariantly refine the return type of the foo() and bar() methods like this: class ConcreteFoo : public Foo { public: ConcreteBar* bar(); }; class ConcreteBar : public Bar { public: ConcreteFoo* foo(); }; This won't compile since our beloved

Array covariance in F#

非 Y 不嫁゛ 提交于 2019-12-05 10:03:30
Since .NET arrays are covariant, the following works in C#: var strArray = new string[0]; object[] objArray = strArray; In F#, given an array, 'T[] , what would be the best way to convert it to obj[] , without re-creating the array (e.g., Array.map box )? I'm using (box >> unbox) , but it feels sloppy. box >> unbox seems like a good idea; O(1), and does the job, apparently. Consider also not using this CLR mis-feature. ;) As Brian says, there's nothing wrong with box >> unbox , other that the fact that array covariance is inherently broken (e.g. ([| "test" |] |> box |> unbox<obj[]>).[0] <- obj

Why isn't DbSet covariant?

牧云@^-^@ 提交于 2019-12-05 09:16:16
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]'. Gert Arnold It looks like they could be covariant. But there is a host of differences between in-memory programming and programming against a query provider. For Entity Framework to

C# Generics Interface Covariance

ⅰ亾dé卋堺 提交于 2019-12-05 09:02:02
I'm not sure what's going on here but I'm getting a compiler error using the following code: namespace SO { interface IUser<PostType> { PostType Post { get; set; } } interface IPost<UserType> { UserType User { get; set; } } class User : IUser<Post> { //Implementation } class Post : IPost<User> { //Implementation } class SomeOtherClass { // Compiler Error: Cannot implicitly convert type 'SO.User' to // 'SO.IUser<SO.IPost<SO.User>>'. An explicit conversion exists // (are you missing a cast?) IUser<IPost<User>> user = new User(); //Works Fine IUser<Post> user = new User(); } } Why am I getting an

How is the datatype of type parameter decided in covariance and contravariance?

◇◆丶佛笑我妖孽 提交于 2019-12-05 08:24:40
I was reading the book Java Generics and Collections By Maurice Naftalin, Philip Wadler, and within the first two chapters I ended up in having my head messed up with doubts. I was not able to figure out the answers. In the call: public static <T> void copy(List<? super T> dst, List<? extends T> src) { for (int i = 0; i < src.size(); i++) { dst.set(i, src.get(i)); } } List<Object> objs = Arrays.<Object>asList(2, 3.14, "four"); List<Integer> ints = Arrays.asList(5, 6); Collections.copy(objs, ints); assert objs.toString().equals("[5, 6, four]"); During call to the function 'copy': 1st parameter:

Is there a generic Task.WaitAll?

痞子三分冷 提交于 2019-12-05 08:18:58
问题 I start a few parallel tasks, like this: var tasks = Enumerable.Range(1, 500) .Select(i => Task.Factory.StartNew<int>(ProduceSomeMagicIntValue)) .ToArray(); and then join them with Task.WaitAll(tasks); On this last line I get a blue squiggly marker under tasks , with a warning message: Co-variant array conversion from Task[] to Task[] can cause run-time exception on write operation. I understand why I get this message, but is there a way around that? (for example, like a generic version of

Running (one pass) calculation of covariance

只愿长相守 提交于 2019-12-05 06:07:04
I got a set of 3d vectors (x,y,z), and I want to calculate the covariance matrix without storing the vectors. I will do it in C#, but eventually I will implement it in C on a microcontroller, so I need the algorithm in itself, and not a library. Pseudocode would be great also. The formula is simple if you have Matrix and Vector classes at hand: Vector mean; Matrix covariance; for (int i = 0; i < points.size(); ++i) { Vector diff = points[i] - mean; mean += diff / (i + 1); covariance += diff * diff.transpose() * i / (i + 1); } covariance *= 1 / points.size() I personally always prefer this

Assigning IEnumerable (Covariance)

跟風遠走 提交于 2019-12-05 05:40:01
Since IEnumerable has a covariant parameter in C# 4.0 I am confused how it is behaving in the following code. public class Test { IEnumerable<IFoo> foos; public void DoTestOne<H>(IEnumerable<H> bars) where H : IFoo { foos = bars; } public void DoTestTwo(IEnumerable<IBar> bars) { foos = bars; } } public interface IFoo { } public interface IBar : IFoo { } So basically the DoTestOne method doesn't compile while DoTestTwo does. In addition to why it doesn't work, if anyone knows how I can achieve the effect of DoTestOne (assigning an IEnumberable<H> where H : IFoo to an IEnumberable<IFoo> ) I