https://kotlinlang.org logo
Title
b

bbaldino

07/17/2019, 6:27 AM
i have a generic class and want to define an inner interface to define a handler for that type (which would include the generic). i.e.:
class Foo<T> {
   ...
   inner interface FooEventHandler {
     fun event(value: T)
   }
  }
but it looks like this isn't possible (it says
inner
can't be used with
interface
). is there a good way to accomplish something like this? i don't want the user to have to define the generic type in 2 places (once for Foo and once for instantiating a FooEventHandler, because it could only lead to a mistake)
on top of that, i think implementing the interface would be impossible (`object : myFoo.FooEventHandler`doesn't work i don't think) so just wondering if there's another good strategy for this sort of thing
k

karelpeeters

07/17/2019, 7:51 AM
inner class
means instances of the class always store a reference to the base class, and of course that isn't possible with interfaces. You'll have to repeat the generic argument.
b

bbaldino

07/17/2019, 3:38 PM
makes sense, thanks @karelpeeters