559 questions
5 votes
2 answers
155 views
Return-type contravariance in Java
I have a generic map type where the keys have type K and the values have type V. I would like to provide a method Optional<? super V> get(K key), however it doesn't seem to work as expected. The ...
3 votes
0 answers
61 views
Variance Annotation for an independent Method level type parameters in scala?
Consider this example from book Programming in Scala, 5th Edition by Martin Odersky (Chapter 18 · Type Parameterization) Here - the following class Cat is described: abstract class Cat[-T, +U]: def ...
0 votes
1 answer
63 views
Is there an exception in which it's possible to read a contravariance (in kotlin)?
I was told that it could be possible to read a contravariance if used "Any?" as in fun processList(list: List<in Class1>) { val item: Any? = list[0] println(item) } To test ...
0 votes
1 answer
118 views
contravariance in generics - what for in that case
I dont know what for it is "in" TCommand keyword in that interface where TCommand is a class with a few properies needed for handler. Is it needed? What it gives in that context ? or maybe &...
2 votes
0 answers
104 views
Regarding the contravariance of fn(&'a i32) -> ()
As is well known, contravariance is quite rare in Rust. According to the Rust Reference on variance, only fn(T) -> () is contravariant on T. I’ve never fully understood how the following conclusion ...
1 vote
0 answers
50 views
I am not able to typecast the derived class object to Base interface<Base> C# [duplicate]
Can any body help me in succesfully typecasting the derived class object which in real application created from Dependency Injection and retrieved from builder.Services. I am typecasting it to base ...
1 vote
0 answers
32 views
Typescript fails to warn against an invalid method override (because it fails to detect contravariant relationship) [duplicate]
I think TS fails to warn against an invalid method override. In the following code, Dog extends Animal. But Dog is a consumer of DogFood, a subclass of Food, and Animal is a consumer of Food. So the ...
5 votes
0 answers
98 views
Is the inconsistency between conversions allowed by std::not_fn and std::function constructor intended?
If you have something like this, struct Foo { }; constexpr auto pred = [](){ return Foo{}; }; calling std::not_fn(pred) will fail to compile, expectedly. However, adding an explicit conversion ...
5 votes
1 answer
244 views
Why the functions Contravariance with their paramaters in typescript?
I have recently studied the generic concepts in Typescript. I have a problem with understanding "Why do the functions contravariance with their parameters?". I know that: Covariance is ifT ...
1 vote
4 answers
103 views
Getting generic class assigned to given object type
I'm writing a C# program that calculates total worth of financial assets. Each asset is represented by asset ID. There are calculator classes specialized for calculating worth of specific asset ID. ...
0 votes
1 answer
221 views
Examples of variance (Jon Skeet book)
Is it correct, that second example in the book has direction from dynamic to object? First example has direction from string to object, I thought that second one will be from object to dynamic. To ...
1 vote
1 answer
456 views
Why is `Callable` generic type contravariant in the arguments?
TL;DR: Why is the Callable generic type contravariant in the arguments as stated by the PEP 483 and how is my analysis of that question (in)accurate? (Said analysis at the bottom of the post) Context:...
1 vote
0 answers
54 views
Error when casting System.Action using contravariance in C#
I have an interface with an event with the sender as the argument. public interface IShootAttackChild { public event System.Action<IShootAttackChild> OnDestroy; /* and some functions */ }...
0 votes
0 answers
29 views
How to properly define a generic constructor type in TypeScript?
I am trying to implement a dependency injection system, so that instead of instantiating all the in-depth structure of dependencies manually …: const config = new Config() const serverStarter = new ...
1 vote
0 answers
72 views
Why do I have to type the input as `never` in a generic function? [duplicate]
When I want to represent a type for *some* function, an intuitive way would be to say that it accepts an unknown argument (let's agree that all functions accept one argument): type SomeFunction = (...