How to reference a generic type parameter in a nes...
# coroutines
d
How to reference a generic type parameter in a nested class? I would like to be able to do this
Copy code
class C<X> {
    class N {
        fun foo(c: C<X>) {}
    }
}
That's apparently not possible. Why? I'm able to workaround it by making
foo
an extension function but that doesn't feel right.
s
If it's an inner class it works.
Copy code
class C<X> {
  inner class N {
    fun foo(c: C<X>) {}
  }
}
This question was probably more suited for #general-advice, it's not related to #coroutines.
d
oh, sorry - wrong channel of course 🙂
Hm, unfortunately my classes are data classes, and inner isn't allowed there...
m
The other solution is to make N generic also. The two generics will be independent, but if C only works with N of the same type, and N only works with C of the same type, you get your type safety.
354 Views