[generics] hello :wave: Is there a nice way to avo...
# announcements
d
[generics] hello 👋 Is there a nice way to avoid the extra cast below?
Copy code
fun <T : AbstractStub<*>> getClient(stubFactory: (Channel) -> T): T {
    val client = stubFactory.invoke(configuredChannel)
    return if (deadlineMs != null) {
        // returns AbstractStub<S>
        client.withDeadlineAfter(deadlineMs, TimeUnit.MILLISECONDS) as T
    } else {
        client
    }
}
was playing around with inlining the
T
but cannot get it to work --- doing the chaining call from from the caller works as expected
Copy code
val client: HelloGrpc.HelloBlockingStub = getClient { HelloGrpc.newBlockingStub(it) }.withDeadlineAfter(1000, TimeUnit.MILLISECONDS)
j
does it work if you change the generic signature to
fun <T : AbstractStub<T>> getClient(stubFactory: () -> T): T {
? That should make it an
AbstractStub<T>
instead of an
AbstractStub<S>
d
doh
indeed
thanks!
👍 1
a
Why have this function at all?
d
that function is within a channel factory that automatically configures the underlying channel based on the properties
i got some standard enterprise configs that i need to apply on the underlying channel (e.g. tracing, metrics, circuit breakers, etc) so it was just a convenient way of creating clients using the configured channel
technically could expose the channel as a bean and have it consumed directly
might look into this