3

Having the following definition:

public class Generic<T>
{
    public class Nested { }
}

And given that ECMA ref §25.1 states:

Any class nested inside a generic class declaration or a generic struct declaration (§25.2) is itself a generic class declaration, since type parameters for the containing type shall be supplied to create a constructed type.

I understand that Nested requires the type parameter in order to be instantiated.

I can obtain the generic Type with typeof:

var type = typeof(Generic<>.Nested); 
// type.FullName: Namespace.Generic`1+Nested

Is there any way I can use it as a type parameter for a generic method such as the following?

var tmp = Enumerable.Empty<Generic<>.Nested>(); 
// Error: Unexpected use of an unbound generic

As stated before, from the ECMA specification I understand that all the type parameters must be satisfied before instancing but no objects are being created here. Furthermore, the Nested class does not make use of the type parameters in any way: I simply would like to define it nested for code organization purposes.

8
  • What would you do with an empty sequence of non-specified nested classes? I don't see any way around the limitation. The fact that Nested doesn't utilize T and that you're currently prototyping with an empty sequence do not appear to be relevant. For a fully constructible type for the sequence, you'll need to supply the type parameter. Commented Apr 14, 2016 at 8:30
  • @AnthonyPegram the example is poorly chosen, sorry. The actual motivation is defining message classes inside generic Akka.Net actors. Thus, I just want to define those nested classes where they belong, but they do not require type parameters in any way. Commented Apr 14, 2016 at 8:32
  • I assume you want to work with any instance (or list of) the nested class without regard to the containing outer class? I would suggest as an alternative to define an interface, have your nested class inherit from that interface, and then work via references (including lists) of that interface. Something like pastebin.com/xLQDKudb Commented Apr 14, 2016 at 8:37
  • Do you want to declare var tmp = Enumerable.Empty<Generic<>.Nested>(); outside of the parent class? Otherwise var tmp = Enumerable.Empty<Nested>(); should work. Commented Apr 14, 2016 at 8:39
  • Another approach is to create a builder function in your parent class that will build you an IEnumarable instance of your child class? Commented Apr 14, 2016 at 8:41

2 Answers 2

3

No, you can't do that. As you said

all the type parameters must be satisfied before instancing

and though no instance of Generic<>.Nested is actually generated

  • the compiler does not know the semantics of Empty (so doesn't know that no instance of Generic<>.Nested is created)
  • and the main problem: you do want to create an instance of IEnumerabe<Generic<>.Nested>, which would be a type with "unsatisfied type parameters", too
Sign up to request clarification or add additional context in comments.

Comments

0

I think your main goal will probably be achieved simply by putting the sub class next to the parent in the same namespace.

The namespace is really what should be organising the code in the way you are talking about in the comments under your post.

You should really only be defining a class as a sub class it only the parent class is going to be using it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.