SwiftedMind
SwiftedMind

Reputation: 4317

Use nested type as type parameter for generic subclass

Is the following use of Generics "allowed"/unproblematic in swift? I mean, it compiles, but could this cause problems in some cases? It feels strange to use something from within a class while defining that class.

class MyGeneric<A> {}

class MyClass: MyGeneric<MyClass.NestedType> {
    enum NestedType {}
}

This question might be kind of stupid and the answer might very well be "it compiles, so yes" but it feels quite strange to write it like this. But it would also be nice if it was fine. Makes the code much more organized and concise.

Upvotes: 1

Views: 109

Answers (1)

Alexander
Alexander

Reputation: 63399

It's fine.

MyClass.NestedType doesn't get any special attachment to MyClass, MyGeneric or A. It's just a naming thing.

Constrast this with say, Java, where non-static inner classes are parameterized by their containing classes generic type. E.g. Foo<A>.Nested is a different class from Foo<B>.Nested.

Upvotes: 1

Related Questions