variance

Why is there no parameter contra-variance for overriding?

◇◆丶佛笑我妖孽 提交于 2019-11-27 12:23:35
C++ and Java support return-type covariance when overriding methods. Neither, however, support contra-variance in parameter types - instead, it translates to over loading (Java) or hiding (C++). Why is that ? It seems to me that there is no harm in allowing that. I can find one reason for it in Java - since it has the "choose-the-most-specific-version" mechanism for overloading anyway - but can't think of any reason for C++. Example (Java): class A { public void f(String s) {...} } class B extends A { public void f(Object o) {...} // Why doesn't this override A.f? } On the pure issue of contra

When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

一曲冷凌霜 提交于 2019-11-27 11:40:17
@uncheckedVariance can be used to bridge the gap between Scala's declaration site variance annotations and Java's invariant generics. scala> import java.util.Comparator import java.util.Comparator scala> trait Foo[T] extends Comparator[T] defined trait Foo scala> trait Foo[-T] extends Comparator[T] <console>:5: error: contravariant type T occurs in invariant position in type [-T]java.lang.Object with java.util.Comparator[T] of trait Foo trait Foo[-T] extends Comparator[T] ^ scala> import annotation.unchecked._ import annotation.unchecked._ scala> trait Foo[-T] extends Comparator[T

How does Java's use-site variance compare to C#'s declaration site variance?

白昼怎懂夜的黑 提交于 2019-11-27 06:58:21
My understand is that specifying variance for generics in C# happens at the type declaration level: when you're creating your generic type, you specify the variance for the type arguments. In Java, on the other hand, variance is specified where a generic is used: when you create a variable of some generic type, you specify how its type arguments can vary. What are the pros and cons to each option? Ross Tate I am just going to answer the differences between declaration-site and use-site variance, since, while C# and Java generics differ in many other ways, those differences are mostly

Rolling variance algorithm

允我心安 提交于 2019-11-27 05:00:35
问题 I'm trying to find an efficient, numerically stable algorithm to calculate a rolling variance (for instance, a variance over a 20-period rolling window). I'm aware of the Welford algorithm that efficiently computes the running variance for a stream of numbers (it requires only one pass), but am not sure if this can be adapted for a rolling window. I would also like the solution to avoid the accuracy problems discussed at the top of this article by John D. Cook. A solution in any language is

ref and out parameters in C# and cannot be marked as variant

此生再无相见时 提交于 2019-11-27 01:13:14
What does the statement mean? From here ref and out parameters in C# and cannot be marked as variant. 1) Does it mean that the following can not be done. public class SomeClass<R, A>: IVariant<R, A> { public virtual R DoSomething( ref A args ) { return null; } } 2) Or does it mean I cannot have the following. public delegate R Reader<out R, in A>(A arg, string s); public static void AssignReadFromPeonMethodToDelegate(ref Reader<object, Peon> pReader) { pReader = ReadFromPeon; } static object ReadFromPeon(Peon p, string propertyName) { return p.GetType().GetField(propertyName).GetValue(p); }

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

Why is there no parameter contra-variance for overriding?

若如初见. 提交于 2019-11-26 18:08:57
问题 C++ and Java support return-type covariance when overriding methods. Neither, however, support contra-variance in parameter types - instead, it translates to over loading (Java) or hiding (C++). Why is that ? It seems to me that there is no harm in allowing that. I can find one reason for it in Java - since it has the "choose-the-most-specific-version" mechanism for overloading anyway - but can't think of any reason for C++. Example (Java): class A { public void f(String s) {...} } class B

How does Java's use-site variance compare to C#'s declaration site variance?

一笑奈何 提交于 2019-11-26 17:36:29
问题 My understand is that specifying variance for generics in C# happens at the type declaration level: when you're creating your generic type, you specify the variance for the type arguments. In Java, on the other hand, variance is specified where a generic is used: when you create a variable of some generic type, you specify how its type arguments can vary. What are the pros and cons to each option? 回答1: I am just going to answer the differences between declaration-site and use-site variance,

When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

被刻印的时光 ゝ 提交于 2019-11-26 15:40:36
问题 @uncheckedVariance can be used to bridge the gap between Scala's declaration site variance annotations and Java's invariant generics. scala> import java.util.Comparator import java.util.Comparator scala> trait Foo[T] extends Comparator[T] defined trait Foo scala> trait Foo[-T] extends Comparator[T] <console>:5: error: contravariant type T occurs in invariant position in type [-T]java.lang.Object with java.util.Comparator[T] of trait Foo trait Foo[-T] extends Comparator[T] ^ scala> import

ref and out parameters in C# and cannot be marked as variant

℡╲_俬逩灬. 提交于 2019-11-26 09:37:01
问题 What does the statement mean? From here ref and out parameters in C# and cannot be marked as variant. 1) Does it mean that the following can not be done. public class SomeClass<R, A>: IVariant<R, A> { public virtual R DoSomething( ref A args ) { return null; } } 2) Or does it mean I cannot have the following. public delegate R Reader<out R, in A>(A arg, string s); public static void AssignReadFromPeonMethodToDelegate(ref Reader<object, Peon> pReader) { pReader = ReadFromPeon; } static object