Questions tagged [nested-generics]

138 questions
38
votes
2 answers

Flatten IEnumerable>; understanding generics

I wrote this extension method (which compiles): public static IEnumerable Flatten(this IEnumerable @this) where T : IEnumerable { foreach (T t in @this) foreach (J j in t) …
Daryl
  • 3,036
  • 3
  • 25
  • 37
27
votes
2 answers

Java Generics Hell

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…
Jeff Axelrod
  • 25,625
  • 29
  • 137
  • 239
22
votes
3 answers

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

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 {…
verdesmarald
  • 11,008
  • 2
  • 38
  • 58
17
votes
5 answers

Java nested generic type mismatch

In the following example: public static void main(String[] args) { List b = new ArrayList(); first(b); second(b); List> a = new ArrayList>(); third(a); fourth(a); // doesnt…
why
  • 1,177
  • 3
  • 11
  • 22
15
votes
1 answer

Why can't nested generic types be inferred?

Given the following classes... public abstract class FooBase where TBar : BarBase{} public abstract class BarBase{} public class Bar1 : BarBase{} public class Foo1 : FooBase {} ...and the following method... public TBar…
Stefan Moser
  • 6,053
  • 7
  • 32
  • 47
14
votes
3 answers
12
votes
1 answer

Java Generics: assignment with nested wildcard parameters

For the following code sample: public static class Abc { } public static class Def { } public static class Ghi { } public void doThis() { List listOne; List> listTwo; List>> listThree; …
sumitsu
  • 1,371
  • 14
  • 30
12
votes
1 answer

Can I pass a complex type structure in Java generics?

I am currently trying to implement an API for a conceptual model using Java interfaces and generics. The model (Transmodel V5.0) is described as an entity-relationship model in great detail, but it does not specify some of the base types used. For…
Peter Becker
  • 8,405
  • 7
  • 38
  • 59
12
votes
2 answers

Nested generic syntax ambiguity >>

Apparently, C# is as susceptible to '>>' lexer dilemma as is C++. This C# code is pretty valid, it compiles and runs just fine: var List = new Dummy("List"); var Nullable = new Dummy("Nullable"); var Guid = new Dummy("Guid"); var x =…
Oleg Mihailik
  • 2,267
  • 2
  • 18
  • 30
11
votes
2 answers

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

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#…
Daryl
  • 3,036
  • 3
  • 25
  • 37
11
votes
3 answers

Nested Type Parameters in Java

This is an example which I made up to be a simplification of my real code, so I apologize if it is a little contrived. What I would like to do is to effectively get two type parameters out of a single nested type argument. I'm pretty sure this is…
Russell Leggett
  • 8,115
  • 3
  • 27
  • 43
9
votes
1 answer

Swift 3.1 Nested Generics Bug with Cyclic Metadata

First of all, thank you for visiting. I'm currently playing with Swift 3.1 Nested Generics and I've encountered an error with initialization. class NestedProduct { enum Gadget { case smartphone case laptop case fridge case…
Bob Lee
  • 667
  • 4
  • 10
9
votes
3 answers

Please help me understand polymorphism when using generics in c#

I am having a problem understanding how polymorphism works when using generics. As an example, I have defined the following program: public interface IMyInterface { void MyMethod(); } public class MyClass : IMyInterface { public void…
Steve Rukuts
  • 8,609
  • 3
  • 43
  • 67
9
votes
7 answers

Recursive Generic and Fluent Interface

tl;dr Trying to implement a hierarchal fluent interface such that I can combine nodes child classes while also the class standalone, but getting type parameter is not within its bound errors. Details I'm trying to achieve a solution so that I can…
SS44
  • 757
  • 1
  • 9
  • 25
8
votes
1 answer

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

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…
World Outsider
  • 293
  • 2
  • 7
1
2 3
9 10