covariance

Confusion with collections of nested generics

为君一笑 提交于 2019-12-11 01:05:43
问题 Please help me understand why add1() and add4() report errors and why add2() and add3() don't. Specifically, please show examples of undesired consequences if the compiler allowed each of these to compile. class InnerTypeConfusion { interface Animal {} class Dog implements Animal {} class Room<T> { void add(T t) {} } void add1(Room<? extends Animal> room) { // Error: The method add(capture#1-of ? extends Animal) in the type // Room<capture#1-of ? extends Animal> is not applicable for the //

Casting to a generic class with interface

家住魔仙堡 提交于 2019-12-10 19:46:16
问题 Updated with input from MarcinJuraszek I have a feeling I'm bumping into a co / contra variance problem here, but I'm not sure I understand how to fix it. I have a class like this: public interface ISomeClass<TEnum, out S> { TEnum Dim { get; } IEnumerable<S> Inc { get; } } public class SomeClass<TEnum, S> : ISomeClass<TEnum, S> where TEnum : struct, IConvertible where S : IMyInterface { public TEnum Dim { get; set; } public IEnumerable<S> Inc { get; set; } } and I have a class that implements

EL and covariant return types

自作多情 提交于 2019-12-10 18:54:07
问题 i have these classes public abstract class Unit { public abstract UnitType getType(); ... } public class Item extends Unit { protected ItemType type; @Override public ItemType getType() { return type; } public void setType(ItemType type) { this.type = type; } ... } and obvoiusly ItemType extends UnitType . and i get: javax.el.PropertyNotWritableException: /WEB-INF/facelets/general.xhtml @23,165 value="#{bean.item.type}": The class 'com.example.Item' does not have a writable property 'type'. i

Covariance Also in 3.5 / 2.0?

女生的网名这么多〃 提交于 2019-12-10 18:05:20
问题 weird problem... Ive implemented covariance from example . My target FW is 4.0. How ever . I wanted to see if it fails on 3.5 /2 -> but it wont. It cant be since covariance is from FW4.0. After changing to 3.0 i Build and see : (+wont fail on runtime) 回答1: Covariance was supported in framework 3, but not implemented in the compiler. You are using compiler 4 targeting framework 3. This is mentioned in an Eric Lippert blog post, I'll see if I can find it.. EDIT To further clarify,: The Common

How to use Guava Optional as “naturally covariant object”

半世苍凉 提交于 2019-12-10 17:14:48
问题 The new Guava 10 Optional states to be naturally covariant and thus may be casted. If I try so it looks a bit ugly: Optional<Integer> opti = Optional.of(42); Optional<Number> optn = (Optional) opti; I like to see some utility function like: static <T> Optional<T> transform(Optional<? extends T> opt, Class<T> clazz); (how to express this as a member function of Optional ?) Is it even possible to define a transformation function object like: static <T> Function<Optional<? extends T>, Optional<T

Covariance in different FW causes Code Break?

﹥>﹥吖頭↗ 提交于 2019-12-10 16:53:39
问题 I saw Jon Skeet's lecture at the NDC 2010 He mentioned something interesting : public Class Base { public void Foo(IEnumerable<string> strings){} } public Class Child:Base { publc void Foo(IEnumerable<object> objects) {} } Main : List<string> lst = new List<string>(); lst.Add("aaa"); Child c = new Child(); c.Foo(lst); With C# 3 it will call : Base.Foo With C# 4 it will call : Child.Foo I know it's because covariance Question : Isn't it a bit code breaking change ? Is there any workaround so

Covariant use of generic Lazy class in C#

≯℡__Kan透↙ 提交于 2019-12-10 16:17:04
问题 Assuming that this applies: public class Cat : Animal { } and assuming that I have a method: public void Feed(Animal animal) { ... } And I can call it this way: var animal = new Cat(); Feed(animal); How can I get this working when Feed is refactored to only support Lazy<Animal> as parameter? I'd like to pass in my var lazyAnimal = new Lazy<Cat>(); somehow. This obviously doesnt work: var lazyAnimal = new Lazy<Cat>(); Feed(lazyAnimal); 回答1: Well, you won't be able to use it exactly as is.

Why does vb.net reject assignment to nested covariant interface as “ambiguous”

a 夏天 提交于 2019-12-10 15:07:00
问题 In the code: Interface ISelf(Of Out TMe) End Interface Class SomeBase Implements ISelf(Of SomeBase) End Class Class SomeDerived Inherits SomeBase Implements ISelf(Of SomeDerived) End Class Module ISelfTester Sub TestISelf() Dim z7 As New SomeDerived Dim z8 As ISelf(Of SomeDerived) Dim z9 As ISelf(Of ISelf(Of SomeDerived)) z8 = z7 z9 = z8 z9 = z7 ' Why is this illegal? End Sub End Module The assignment directly from Z7 to Z9 yields the message "Error 13 Option Strict On does not allow implicit

How to implement generic polymorphism in c#?

谁都会走 提交于 2019-12-10 14:53:08
问题 to avoid confusion I summarised some code: namespace ConsoleApplication1 { class Program { static void Main() { IManager<ISpecificEntity> specificManager = new SpecificEntityManager(); IManager<IIdentifier> manager = (IManager<IIdentifier>) specificManager; manager.DoStuffWith(new SpecificEntity()); } } internal interface IIdentifier { } internal interface ISpecificEntity : IIdentifier { } internal class SpecificEntity : ISpecificEntity { } internal interface IManager<TIdentifier> where

calling derived methods on a baseclass collection

余生颓废 提交于 2019-12-10 14:25:40
问题 I have one abstract class named A, and other classes (B, C, D, E, ...) that implements A. My derived classes are holding values of different types. I also have a list of A objects. abstract class A { } class B : class A { public int val {get;private set;} } class C : class A { public double val {get;private set;} } class D : class A { public string val {get;private set;} } class Program { static void Main(string[] args) { List list = new List { new B(), new C(), new D(), new E() }; // ...