nested-generics

how to declare Class.class with valid generics

柔情痞子 提交于 2019-11-30 22:33:02
Note: purely out of curiosity and not for any actual use case. I'm wondering if there is a way to declare the Class Class object with valid type parameters: Class cc1 = Class.class; //raw type Class<Class> cc2 = Class.class; //now parameter is raw type Class<Class<?>> cc3 = Class.class; //compile error: inconvertible types If Class and Class<?> are interchangeable, why are Class<Class> and Class<Class<?>> not? EDIT: the question can be generalized to an issue of nested raw type parameters. For example: ArrayList<ArrayList<?>> lst = new ArrayList<ArrayList>(); //same compile error EDIT2: I

Implementing nested generic Interfaces

ⅰ亾dé卋堺 提交于 2019-11-30 06:49:16
问题 I have the following Classes / Interfaces: // Model public class A : IA { } // ModelLogic public class B : IB<A> { } // Model Interface public interface IA { } // ModelLogic Interface public interface IB<T> where T : IA { } I try to create a new instance using the following code: IB<IA> foo = new B(); I am getting the following error: Cannot implicitly convert type 'B' to 'IB<IA>'. An explicit conversion exists (are you missing a cast?) Can someone please explain why this is not possible? 回答1

Implementing nested generic Interfaces

人盡茶涼 提交于 2019-11-28 21:29:50
I have the following Classes / Interfaces: // Model public class A : IA { } // ModelLogic public class B : IB<A> { } // Model Interface public interface IA { } // ModelLogic Interface public interface IB<T> where T : IA { } I try to create a new instance using the following code: IB<IA> foo = new B(); I am getting the following error: Cannot implicitly convert type 'B' to 'IB<IA>'. An explicit conversion exists (are you missing a cast?) Can someone please explain why this is not possible? OK, let's replace A with Fish , IA with IAnimal , B with Aquarium , and IB<T> with IContainer<T> . And we

Why aren't type constraints part of the method signature?

半城伤御伤魂 提交于 2019-11-28 09:18:36
UPDATE: As of C# 7.3, this should no longer be an issue. From the release notes: When a method group contains some generic methods whose type arguments do not satisfy their constraints, these members are removed from the candidate set. Pre C# 7.3: So I read Eric Lippert's 'Constraints are not part of the signature' , and now I understand that the spec specifies that type constraints are checked AFTER overload resolution, but I'm still not clear on why this MUST be the case. Below is Eric's example: static void Foo<T>(T t) where T : Reptile { } static void Foo(Animal animal) { } static void

Why can't nested generic types be inferred?

爱⌒轻易说出口 提交于 2019-11-28 08:24:05
Given the following classes... public abstract class FooBase<TBar> where TBar : BarBase{} public abstract class BarBase{} public class Bar1 : BarBase{} public class Foo1 : FooBase<Bar1> {} ...and the following method... public TBar DoSomething<TFoo, TBar>(TFoo theFoo) where TFoo : FooBase<TBar> where TBar : BarBase { return default(TBar); } Why can't the following line of code imply the return type? Bar1 myBar = DoSomething(new Foo1()); Instead I have to specify the generic types like this... Bar1 myBar = DoSomething<Foo1, Bar1>(new Foo1()); Method type inference ignores generic constraints on

Java nested generic type mismatch

核能气质少年 提交于 2019-11-27 20:12:28
In the following example: public static void main(String[] args) { List<String> b = new ArrayList<String>(); first(b); second(b); List<List<String>> a = new ArrayList<List<String>>(); third(a); fourth(a); // doesnt work } private static <T> void first(List<T> a){ System.out.println("List of T"); } private static void second(List<?> a){ System.out.println("List of anything "); } private static <T> void third(List<List<T>> a){ System.out.println("List of a List of T "); } private static void fourth(List<List<?>> a){ System.out.println("List of a List of anything "); } Why does the call to second

Why aren't type constraints part of the method signature?

心已入冬 提交于 2019-11-27 05:51:19
问题 UPDATE: As of C# 7.3, this should no longer be an issue. From the release notes: When a method group contains some generic methods whose type arguments do not satisfy their constraints, these members are removed from the candidate set. Pre C# 7.3: So I read Eric Lippert's 'Constraints are not part of the signature', and now I understand that the spec specifies that type constraints are checked AFTER overload resolution, but I'm still not clear on why this MUST be the case. Below is Eric's

Why can't nested generic types be inferred?

瘦欲@ 提交于 2019-11-27 02:11:17
问题 Given the following classes... public abstract class FooBase<TBar> where TBar : BarBase{} public abstract class BarBase{} public class Bar1 : BarBase{} public class Foo1 : FooBase<Bar1> {} ...and the following method... public TBar DoSomething<TFoo, TBar>(TFoo theFoo) where TFoo : FooBase<TBar> where TBar : BarBase { return default(TBar); } Why can't the following line of code imply the return type? Bar1 myBar = DoSomething(new Foo1()); Instead I have to specify the generic types like this...