covariance

Workaround for lack of return type covariance when overriding virtual methods

拥有回忆 提交于 2019-12-10 02:58:21
问题 is there any way to 'hack' or 'coerce' covariant overrides in to C#? For example: public class Alpha { public virtual Alpha DoSomething() { return AlphaFactory.GetAlphaFromSomewhere(); } } public class Beta : Alpha { public override Beta DoSomething() { return BetaFactory.GetBetaFromSomewhere(); } } Unfortunately, C# doesn't support this (which seems a bit ridiculous, but that's neither here nor there). I thought I might have an answer with method hiding: new public Beta DoSomething() {

Implementing a method inside a Scala parameterized class with a covariant type

故事扮演 提交于 2019-12-09 22:50:02
问题 I've read a few tutorials including the main Scala documentation regarding method signatures of covariant types. Suppose I have the following abstract class: abstract class List[+A] { def head: A def tail: List[A] def isEmpty: Boolean def add[B >: A](element: B): List[B] protected def printElements: String override def toString: String = "[" + printElements + "]" } My question concerns the signature of the add() method. Why is it necessary to declare it that way? We are passing in a parameter

Contravariance in Expressions

一曲冷凌霜 提交于 2019-12-09 16:06:54
问题 I'm trying to create a Generic Action Delegate delegate void ActionPredicate<in T1, in T2>(T1 t1, T2 t2); and public static ActionPredicate<T,string> GetSetterAction<T>(string fieldName) { ParameterExpression targetExpr = Expression.Parameter(typeof(T), "Target"); MemberExpression fieldExpr = Expression.Property(targetExpr, fieldName); ParameterExpression valueExpr = Expression.Parameter(typeof(string), "value"); MethodCallExpression convertExpr = Expression.Call(typeof(Convert), "ChangeType"

Can/should Task<TResult> be wrapped in a C# 5.0 awaitable which is covariant in TResult?

℡╲_俬逩灬. 提交于 2019-12-09 15:03:02
问题 I'm really enjoying working with C# 5.0 asynchronous programming. However, there are a few places where updating old code to be consistent with the TAP model is causing problems for me. Here's one of them - I'm not sure exactly why Task<TResult> is not covariant in TResult, but it's causing problems for me when trying to update a covariant interface to move from a synchronous to an asychronous pattern: Old code: public interface IInitializable<out T> // ** out generic modifier ** { ///

Interfaces inheritance in C#

牧云@^-^@ 提交于 2019-12-09 09:20:35
问题 I'm trying to overrun quite big (for me) problem that I came across while writing my application. Look at this, please (I will try to shorten the code for simplicity): I have root interface called IRepository<T> . Next, IBookRepository : IRepository<Book> Next, concrete class that implements it: BookRepository : IBookRepository In the RepositoryManager class I declared private IRepository<IRepoItem> currentRepo; IRepoItem is an interface that is implemented by Book class. Now, when I try to

Scala contravariance and covariance

戏子无情 提交于 2019-12-09 07:44:39
问题 I am playing around with the typesystem of scala and found a strange case. I have a strong reason to believe, I don't understand covariant and covariance. This is my problem case: I have two classes, Point and ColorPoint, which is a subclass of Point. class Point(val x : Int, val y : Int) class ColorPoint(x : Int, y : Int, val red : Int, val green : Int, val blue : Int) extends Point(x,y) This class casts B to A, while B should be a supertype of A: class CoVariance[+A]{ def cast[B >: A](x : B

Why does this covariance declaration compile? [duplicate]

吃可爱长大的小学妹 提交于 2019-12-08 19:23:04
问题 This question already has answers here : C# variance annotation of a type parameter, constrained to be value type (2 answers) Closed 6 years ago . Consider this interface: interface Test<out T> where T : struct { } It compiles without errors or warnings. As discussed in this question, and mentioned in the Covariance and Contravariance FAQ: Variance is supported only if a type parameter is a reference type. So why does the above interface compile? It would make sense to fail (or at least warn)

Workaround for Scala RDD not being covariant

强颜欢笑 提交于 2019-12-08 17:40:06
问题 I'm trying to write a function to operate on RDD[Seq[String]] objects, e.g.: def foo(rdd: RDD[Seq[String]]) = { println("hi") } This function cannot be called on objects of type RDD[Array[String]]: val testRdd : RDD[Array[String]] = sc.textFile("somefile").map(_.split("\\|", -1)) foo(testRdd) -> error: type mismatch; found : org.apache.spark.rdd.RDD[Array[String]] required: org.apache.spark.rdd.RDD[Seq[String]] I guess that's because RDD isn't covariant. I've tried a bunch of definitions of

TypeScript: subtyping and covariant argument types

懵懂的女人 提交于 2019-12-08 17:40:03
问题 Common sense suggests that subtyping should be covariant with respect to return type but contravariant with respect to argument types. So, the following should be rejected, because of the strictly covariant argument type of E.f : interface C { f (o: C): void } interface D extends C { g (): void // give D an extra service } class E implements C { // implement f with a version which makes stronger assumptions f (o: D): void { o.g() // rely on the extra service promised by D } } // E doesn't

Lower type bound on Scala field in mutable, covariant class?

懵懂的女人 提交于 2019-12-08 17:13:27
问题 I want to create a covariant class which is mutable, so I need to add a lower type bound to the setter method. But I also want the setter method to set a field, so I guess the field needs to have the same type bound? class Thing[+F](initialValue: F) { private[this] var secondValue: Option[G >: F] = None def setSecondValue[G >: F](v: G) = { this.secondValue = Some(v) } } The method compiles fine. But the field called secondValue doesn't compile at all, with the error message: Multiple markers