nested-generics

Higher order (or recursive?) generic type parameters in kotlin

故事扮演 提交于 2019-12-05 03:02:16
I'm prototyping some highly declarative code, and the type inference and safety that comes with Kotlin helps a lot. One of the goals is making extensions (subclasses) of the primary types stupidly easy to implement. In order to maintain rich type inference and expressiveness, I've found some success in defining generic extension functions projected against subclasses. All the type information of subclass methods with none of the extra subclass implementation, it's great. So I'm trying to write a rich generic function that maintains as much type information as possible. The issue creeps up with

Flatten IEnumerable<IEnumerable<>>; understanding generics

独自空忆成欢 提交于 2019-12-03 23:27:09
I wrote this extension method (which compiles): public static IEnumerable<J> Flatten<T, J>(this IEnumerable<T> @this) where T : IEnumerable<J> { foreach (T t in @this) foreach (J j in t) yield return j; } The code below causes a compile time error (no suitable method found), why? : IEnumerable<IEnumerable<int>> foo = new int[2][]; var bar = foo.Flatten(); If I implement the extension like below, I get no compile time error: public static IEnumerable<J> Flatten<J>(this IEnumerable<IEnumerable<J>> @this) { foreach (IEnumerable<J> js in @this) foreach (J j in js) yield return j; } Edit(2) : This

how to declare Class.class with valid generics

孤者浪人 提交于 2019-12-03 21:04:19
问题 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.

Nested Generics: Why can't the compiler infer the type arguments in this case?

家住魔仙堡 提交于 2019-12-03 04:55:51
问题 I was playing around with a hobby project when I came across a type-inference error I didn't understand. I have simplified it to the following trivial example. I have the following classes and functions: class Foo { } class Bar { } class Baz { } static T2 F<T1, T2>(Func<T1, T2> f) { return default(T2); } static T3 G<T1, T2, T3>(Func<T1, Func<T2, T3>> f) { return default(T3); } Now consider the following examples: // 1. F with explicit type arguments - Fine F<Foo, Bar>(x => new Bar()); // 2. F

Java Generics Hell

孤街醉人 提交于 2019-12-03 03:42:06
问题 I suspect this has been asked here (and answered) before, but I don't know how to name the problem. Why can I express the wildcards without problem only when I'm not passing the class itself? It all boils down to this code. Everything works as expected except for the call to genericsHell(ShapeSaver.class) : interface Shape { } interface Circle extends Shape { } interface ShapeProcessor<T extends Shape> { } class CircleDrawer implements ShapeProcessor<Circle> { } class ShapeSaver<T extends

Nested Generics: Why can't the compiler infer the type arguments in this case?

ⅰ亾dé卋堺 提交于 2019-12-02 18:09:06
I was playing around with a hobby project when I came across a type-inference error I didn't understand. I have simplified it to the following trivial example. I have the following classes and functions: class Foo { } class Bar { } class Baz { } static T2 F<T1, T2>(Func<T1, T2> f) { return default(T2); } static T3 G<T1, T2, T3>(Func<T1, Func<T2, T3>> f) { return default(T3); } Now consider the following examples: // 1. F with explicit type arguments - Fine F<Foo, Bar>(x => new Bar()); // 2. F with implicit type arguments - Also fine, compiler infers <Foo, Bar> F((Foo x) => new Bar()); // 3. G

How to get the .class of a Generics defined entity? [duplicate]

家住魔仙堡 提交于 2019-12-01 22:50:10
问题 This question already has answers here : Java - Generics with lists (5 answers) Closed 4 years ago . So how can I do the following in Java? List<String> strings = new ArrayList<String>(); JAXBElement<List<String>> jax = new JAXBElement<List<String>>(new QName("strings"), List<String>.class, strings); The issue specifically occurs at List.class and the error returned is: Multiple markers at this line - List cannot be resolved to a variable - String cannot be resolved to a variable - Syntax

How to get the .class of a Generics defined entity? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-01 20:33:05
This question already has an answer here: Java - Generics with lists 5 answers So how can I do the following in Java? List<String> strings = new ArrayList<String>(); JAXBElement<List<String>> jax = new JAXBElement<List<String>>(new QName("strings"), List<String>.class, strings); The issue specifically occurs at List.class and the error returned is: Multiple markers at this line - List cannot be resolved to a variable - String cannot be resolved to a variable - Syntax error on token ">", void expected after this token You can't. The closest approximation to what you want is to use (Class<List

How do I get this system of nested generic parameters working?

泪湿孤枕 提交于 2019-12-01 11:08:13
So I'm trying to get a reasonably complicated system working. Here's the basics of what I'm attempting. Rules: abstract class Rule { // stuff } class ExampleRule extends Rule { // stuff } Handlers: abstract class RuleHandler<T extends Rule> { Class<T> clazz; RuleHandler(Class<T> forClass) { this.clazz = forClass; } abstract void doStuff(T rule); } class ExampleRuleHandler extends RuleHandler<ExampleRule> { ExampleRuleHandler() { super(ExampleRule.class); } void doStuff(ExampleRule rule) { // stuff } } And tying them together: class HandlerDispatcher { Map<Class<? extends Rule>, RuleHandler<?

How do I get this system of nested generic parameters working?

醉酒当歌 提交于 2019-12-01 09:38:48
问题 So I'm trying to get a reasonably complicated system working. Here's the basics of what I'm attempting. Rules: abstract class Rule { // stuff } class ExampleRule extends Rule { // stuff } Handlers: abstract class RuleHandler<T extends Rule> { Class<T> clazz; RuleHandler(Class<T> forClass) { this.clazz = forClass; } abstract void doStuff(T rule); } class ExampleRuleHandler extends RuleHandler<ExampleRule> { ExampleRuleHandler() { super(ExampleRule.class); } void doStuff(ExampleRule rule) { //