contravariance

The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference

耗尽温柔 提交于 2021-01-27 07:51:32
问题 I'm trying to create a generic to simplify my codes (it's a web api project), but at somehow it's ended up becoming more complicated than I expected. What I'm trying to implement is something like this: To simplify my whole real code, this is what I've written: public interface IDatabaseTable { } public class ReceiptIndex: IDatabaseTable { } public interface IBackend<T> where T: IDatabaseTable { } public class Receipts : IBackend<ReceiptIndex> { } public class Generic<T> : SyncTwoWayXI,

The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference

坚强是说给别人听的谎言 提交于 2021-01-27 07:51:03
问题 I'm trying to create a generic to simplify my codes (it's a web api project), but at somehow it's ended up becoming more complicated than I expected. What I'm trying to implement is something like this: To simplify my whole real code, this is what I've written: public interface IDatabaseTable { } public class ReceiptIndex: IDatabaseTable { } public interface IBackend<T> where T: IDatabaseTable { } public class Receipts : IBackend<ReceiptIndex> { } public class Generic<T> : SyncTwoWayXI,

The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference

微笑、不失礼 提交于 2021-01-27 07:46:45
问题 I'm trying to create a generic to simplify my codes (it's a web api project), but at somehow it's ended up becoming more complicated than I expected. What I'm trying to implement is something like this: To simplify my whole real code, this is what I've written: public interface IDatabaseTable { } public class ReceiptIndex: IDatabaseTable { } public interface IBackend<T> where T: IDatabaseTable { } public class Receipts : IBackend<ReceiptIndex> { } public class Generic<T> : SyncTwoWayXI,

Implicit resolution fails for a contravariant type with a type bound

核能气质少年 提交于 2020-06-26 14:59:27
问题 The following code compiles: class X[U, T <: U] object X { implicit def genericX[U, T <: U]: X[U, T] = new X[U, T] } implicitly[X[String, String]] // compiles When we make T covariant ( class X[U, +T <: U] ) it compile as well: class X[U, +T <: U] object X { implicit def genericX[U, T <: U]: X[U, T] = new X[U, T] } implicitly[X[String, String]] // compiles When we make T contravariant ( class X[U, -T <: U] ), compiler fails to materilize implicitly[X[String, String]] . Strangely enough, it is

IList using covariance and contravariance in c#, is this possible?

依然范特西╮ 提交于 2020-02-03 05:17:45
问题 would this be possible? (I don't have vs. 2010, so I can't try it myself, sorry) public interface IComplexList<out TOutput, in TInput> where TOutput : TInput { public IEnumerator<TOutput> GetEnumerator(); public void Add(TInput item); } public interface IList<T> : IComplexList<T, T> { } If I get it right, you could use this to actually implement covariance and contravariance in the same interface. 回答1: No, you can't. In your example IList<T> is invariant. IList<T> would require to declare in

Why are contravariant type parameters in function parameters considered in “out” position?

纵然是瞬间 提交于 2020-02-02 11:21:49
问题 Hard for me to describe in english, but here's the issue: class Consumer<in T> { fun consume(t: T) {} } class Accepter<in T>() { // ERROR: Type parameter T is declared as 'in' but occurs in 'out' position in type Consumer<T> fun acceptWith(value: T, consumer: Consumer<T>) {} } It can be fixed like this: fun <U : T> acceptWith(value: T, consumer: Consumer<U>) {} But I don't understand the issue. It doesn't seem unsafe to allow Consumer<T> . Can someone explain this? 回答1: The argument position

Understanding scala's _ vs Any/Nothing

こ雲淡風輕ζ 提交于 2020-01-24 02:07:59
问题 If a class has a convariant type parameter such as Iterable[+A], is there any difference between declaring def foo(bar: Iterable[_]) and def foo(bar: Iterable[Any]) ? If a class has a contravariant type parameter such as Growable[-A], is there any difference between declaring def foo(bar: Growable[_]) and def foo(bar: Growable[Nothing]) ? 回答1: It does make a little difference when generic parameter is bounded. For example, if you had class BoundedIterable[+A <: Something] class

In C#, are event handler arguments contravariant?

白昼怎懂夜的黑 提交于 2020-01-15 09:14:24
问题 If I have a class that raises an event, with (e.g.) FrobbingEventArgs, am I allowed to handle it with a method that takes EventArgs? Here's some code: class Program { static void Main(string[] args) { Frobber frobber = new Frobber(); frobber.Frobbing += FrobberOnFrobbing; frobber.Frob(); } private static void FrobberOnFrobbing(object sender, EventArgs e) { // Do something interesting. Note that the parameter is 'EventArgs'. } } internal class Frobber { public event EventHandler

Scala - Co/Contra-Variance as applied to implicit parameter selection

你离开我真会死。 提交于 2020-01-14 07:13:29
问题 I've got a trait like this: trait CanFold[-T, R] { def sum(acc: R, elem: T): R def zero: R } With a function that works with it like this: def sum[A, B](list: Traversable[A])(implicit adder: CanFold[A, B]): B = list.foldLeft(adder.zero)((acc,e) => adder.sum(acc, e)) The intention is to do something like this: implicit def CanFoldSeqs[A] = new CanFold[Traversable[A], Traversable[A]] { def sum(x: Traversable[A], y: Traversable[A]) = x ++ y def zero = Traversable() } sum(List(1, 2, 3) :: List(4,

Variance rules in C#

你说的曾经没有我的故事 提交于 2020-01-12 14:25:28
问题 The Exact rules for variance validity are a bit vague and not specific. I'm going to list the rules for what makes a type valid-covariantly, and attach some queries and personal annotations to each of those rules. A type is valid covariantly if it is: 1) a pointer type, or a non-generic type. Pointers and non-generic types are not variant in C#, except for arrays and non-generic delegates. Generic classes, structs and enums are invariant. Am I right here? 2) An array type T[] where T is valid