1

I'm trying to write an interface for sending generic events to a remote server, along with a listener which returns a response that includes the list of events that was sent/attempted to be sent.

interface Sender<T> {
  interface Response {
    val success: Boolean
    val events: List<T> // unresolved reference
  }

  ...
}

If I change the T to a *, it compiles, but the type doesn't match what I need in the listener.

How can I "inherit" the generic type T in a sub-interface like this?

1
  • 1
    In a nested interface? You can't. Commented Jun 10 at 23:48

1 Answer 1

1

You can't. The only thing you can make work is

interface Sender<T> {
  interface Response<T> {
    val success: Boolean
    val events: List<T> // unresolved reference
  }

  ...
  fun send(response: Response<T>) // or
  fun send(): Response<T>
}

You can't get around repeating the <T>, not with an interface, because you must be able to refer to Sender.Response even when you are not in a Sender object, or you have several Sender objects with different Ts, or whatever.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.