Why does field declaration with duplicated nested type in generic class results in huge source code increase?

后端 未结 3 1406
后悔当初
后悔当初 2021-01-30 16:53

Scenario is very rare, but quite simple: you define a generic class, then create a nested class which inherits from outer class and define a associative field (of self type) wit

3条回答
  •  有刺的猬
    2021-01-30 17:13

    This is not an answer!

    There are many aspects of your question. A little one is: If a type contains (because of inheritance, not allowed otherwise) a nested type with the same name as the type itself, and if that name is used inside the type, what does the name refer to?

    It's hard to express in words, but here's the example I have in mind:

    namespace N
    {
      class Mammal
      {
        // contains nested type of an unfortunate name
        internal interface Giraffe
        {
        }
      }
    
      class Giraffe : Mammal
      {
        Giraffe g;  // what's the fully qualified name of the type of g?
      }
    }
    

    Note: This is easy! No generics! No problem with a class inheriting its own containing class.

    The question here is, what is the type of g? Is it N.Giraffe (a class), or is it N.Giraffe.Giraffe (an interface)? The correct answer is the latter. Because to find a meaning for the name Giraffe, one first searches the members of the current type (in this case one finds the interface). Only if no match is found there, one proceeds to the members of the current namespace (where the current type would be found).

提交回复
热议问题