gregorbg
04/09/2019, 6:56 AMinterface FancyConnector<T : Transcoder<I, O>> : RawHttpConnector {
val transcoder: T
fun send(entity: I) = sendRaw(this.transcoder.encode(entity))
fun receive(): O = this.transcoder.decode(receiveRaw())
}
interface Transcoder<I, O> {
fun encode(entity: I): HttpBytes
fun decode(raw: HttpBytes): O
}
How can I make the compiler recognise the I
and O
type generics? Or alternatively, how can I define my interfaces so that FancyConnector
doesn't need to know the internal types of Transcoder
?
EDIT: Imagine there is a class IdentityTranscoder<E> : Transcoder<E, E>
. I want to be able to use it like class MyConnector<IdentityTranscoder<Foo>>
instead of having to specify class MyConnector<Foo, Foo, IdentityTranscoder<Foo>>
spand
04/09/2019, 7:25 AMgregorbg
04/09/2019, 7:27 AMFancyConnector
I only want to pass a Transcoder
without the user having to worry what it actually encodes/decodes tospand
04/09/2019, 7:29 AMinterface FancyConnector<I, O, T : Transcoder<I, O>>
isnt what you want then ?gregorbg
04/09/2019, 7:43 AMclass OneToManyTranscoder<E> : Transcoder<E, List<E>>
that can be used like FancyConnector<OneToManyTranscoder<Foo>>
(what I want) instead of having to (redundantly) mention FancyConnector<Foo, List<Foo>, OneToManyTranscoder<Foo>>
(what you just suggested)spand
04/09/2019, 8:03 AMFancyConnector
, not when subclassing:
class AConnector<I, O, T : Transcoder<I, O>> : FancyConnector<I, O, T> {
override val transcoder: T
get() = TODO("not implemented")
}
class ATranscoder : Transcoder<String, String> {
override fun encode(entity: String): String {
throw NotImplementedError()
}
override fun decode(raw: String): String {
throw NotImplementedError()
}
}
fun <I, O, T : Transcoder<I, O>> deduceIAndO(trans: T) : FancyConnector<I, O, T> {
return AConnector()
}
val foo = deduceIAndO(ATranscoder())
gregorbg
04/09/2019, 9:37 AM