contravariance

What is an example of contravariant use in Rust?

醉酒当歌 提交于 2019-12-02 10:18:26
In the Nomicon's section about subtyping , it says contravariance is available for a function pointer type. However, I can't find any good examples of this. I tried to code a struct with a function pointer, but the contravariance doesn't seem to work. What is a code example of this? Rust's notion of subtyping only applies to lifetimes . Searching for the term "contra" on the page you linked has numerous relevant paragraphs: Actually witnessing contravariance is quite difficult in Rust, though it does in fact exist. NOTE: the only source of contravariance in the language is the arguments to a

Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work?

痴心易碎 提交于 2019-12-01 22:53:46
Following on from this question , can someone explain the following in Scala: class Slot[+T] (var some: T) { // DOES NOT COMPILE // "COVARIANT parameter in CONTRAVARIANT position" } I understand the distinction between +T and T in the type declaration (it compiles if I use T ). But then how does one actually write a class which is covariant in its type parameter without resorting to creating the thing unparametrized ? How can I ensure that the following can only be created with an instance of T ? class Slot[+T] (var some: Object){ def get() = { some.asInstanceOf[T] } } EDIT - now got this down

Conversion from Func<object,string> to Func<string,string> works but to Func<int,string> fails

跟風遠走 提交于 2019-12-01 18:43:16
I have the following code: static Func<object, string> s_objToString = (x) => x.ToString(); static Func<string, string> s_stringToString = s_objToString; //compiles static Func<int, string> s_intToString = s_objToString; //error The second line compiles but the third line fails to compile with error: Cannot implicitly convert type ' System.Func<object,string> ' to ' System.Func<int,string> ' Why is that? I understand that with genetics although string is derived from object a List<string> does not derive from List<object> , but here object to string works and object to int fails, why? OK let's

Cannot implicitly convert MyType<Foo> to MyType<IFoo>

回眸只為那壹抹淺笑 提交于 2019-12-01 17:33:49
I am not sure if this is a Covariance and Contravariance issue but I cannot get this working. Here is the code: public interface IDto { } public class PaginatedDto<TDto> where TDto : IDto { public int PageIndex { get; set; } public int PageSize { get; set; } public int TotalCount { get; set; } public int TotalPageCount { get; set; } public bool HasNextPage { get; set; } public bool HasPreviousPage { get; set; } public IEnumerable<TDto> Dtos { get; set; } } public class PersonDto : IDto { public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } public int

Parameter must be input-safe error

情到浓时终转凉″ 提交于 2019-12-01 11:00:35
Here is a piece of my code: public interface IA<in TInput> { void Method(IB<TInput> entities); } public interface IB<in T> { } I can't figure out why I get following compile error: "Parameter must be input-safe. Invalid variance: The type parameter |TInput| must be contravariantly valid on "IB< in T>". Any help will be appreciated. dasblinkenlight The designator of contravariance in C# (i.e. in ) is intuitive only at the immediate level, when you make a method that "takes in" a parameter of generic type. Internally, however, contravariance means an inversion of a relation ( Q&A with an

Parameter must be input-safe error

旧城冷巷雨未停 提交于 2019-12-01 09:40:21
问题 Here is a piece of my code: public interface IA<in TInput> { void Method(IB<TInput> entities); } public interface IB<in T> { } I can't figure out why I get following compile error: "Parameter must be input-safe. Invalid variance: The type parameter |TInput| must be contravariantly valid on "IB< in T>". Any help will be appreciated. 回答1: The designator of contravariance in C# (i.e. in ) is intuitive only at the immediate level, when you make a method that "takes in" a parameter of generic type

Why the type position of a method is marked as negative?

江枫思渺然 提交于 2019-12-01 08:44:36
Sorry I have asked some questions like this one, but I still can't get a clear answer, maybe my bad English and unclear expression puzzled the kind people. When I read the "Type Parameterization" in this article: http://www.artima.com/pins1ed/type-parameterization.html , I see there are some explanation about the type positions: As a somewhat contrived example, consider the following class definition, where the variance of several positions is annotated with ^+ (for positive) or ^- (for negative): abstract class Cat[-T, +U] { def meow[W^-](volume: T^-, listener: Cat[U^+, T^-]^-) : Cat[Cat[U^+,

Covariance and Contravariance with C# Arrays [duplicate]

房东的猫 提交于 2019-12-01 05:26:35
This question already has an answer here: Covariance and contravariance real world example 8 answers While reading a section of an article about covariance and contravariance at Wikipedia, I ran into the following, bolded sentence: First consider the array type constructor: from the type Animal we can make the type Animal[] ("array of animals"). Should we treat this as Covariant: a Cat[] is a Animal[] Contravariant: a Animal[] is a Cat[] or neither (invariant)? If we wish to avoid type errors, and the array supports both reading and writing elements, then only the third choice is safe. Clearly

Covariance and Contravariance with C# Arrays [duplicate]

喜夏-厌秋 提交于 2019-12-01 01:21:40
问题 This question already has answers here : Covariance and contravariance real world example (8 answers) Closed 6 years ago . While reading a section of an article about covariance and contravariance at Wikipedia, I ran into the following, bolded sentence: First consider the array type constructor: from the type Animal we can make the type Animal[] ("array of animals"). Should we treat this as Covariant: a Cat[] is a Animal[] Contravariant: a Animal[] is a Cat[] or neither (invariant)? If we

Why method defined like “cons[B >: A](v: B)” accepts argument of type which is not supertype of A?

人盡茶涼 提交于 2019-11-30 15:41:51
问题 I am studying variance in scala right now, and I think I have a good understanding of contravariance. For example given trait List[-A] , I know that List[Int] is a supertype of List[AnyVal] . But say that I have the following trait: trait List[+A] { def cons(hd: A): List[A] } Why is cons parameter type wrong? Why it is necessary to have def cons[B >: A](v: B): List[B] ? For example: val animal_list: List[Animal] = List(tiger, dog) if we call: animal_list.cons(tiger) since Tiger <: Animal ,