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
simon.vergauwen
04/06/2023, 9:28 AM
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
David Kubecka
04/06/2023, 9:30 AM
oh, sorry - wrong channel of course 🙂
David Kubecka
04/06/2023, 9:33 AM
Hm, unfortunately my classes are data classes, and inner isn't allowed there...
m
mkrussel
04/06/2023, 12:23 PM
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.