I've got a ktor method that sets a data class body...
# getting-started
b
I've got a ktor method that sets a data class body on the request; I can either call it using a reified generic inline function like setBody(obj) or with a typeInfo, e.g. setBody(obj, typeInfoObj()) My issue right now is that the http call sits in a method that I don't want to inline and I'm passing a generic object of WrapperT where WrapperT is an interface that holds different payloads; obviously the T in WrapperT will be erased so I need to also carry a TypeInfo around How do I deal with that so that I don't have to manually implement the typeinfo stuff for every implementing class?
Copy code
interface Eventout T : Any {
    val id: String
    val type: String
    val payload: T
}

class Client(
    val client: HttpClient,
    val eventEndpointUrl: String,
) {
    suspend fun T : Any publish(event: EventT): HttpResponse {
        return client.post(eventEndpointUrl) {
            accept(ContentType.Any)
            contentType(ContentType.Application.Json)
            header("ce-id", event.id)
            header("ce-type", event.type)
            setBody(event.payload, event.typeInfo)
        }
    }
}
c
Add
TypeInfo<T>
in
Event
next to payload, so the
inline
is at the construction of the event, and then usage doesn't have to be
👍 1
b
you mean
Copy code
val id: String,
    val type: String,
    val payload: T,
    val typeInfo: TypeInfo,
?
that's what I've got right now, but don't I need to do that for each data class manually then?
c
that's what I've got right now
Is it? That's not in your example
b
yes, this is a simplified example
I was wondering if my design was wrong
I've got something like this but it can't be exported to Js
Copy code
class Eventout T : Any @PublishedApi internal constructor(
    val id: String,
    val type: String,
    val payload: T,
    val typeInfo: TypeInfo,
) {
    companion object {
        inline fun reified T : Any create(
            id: String,
            type: String,
            payload: T,
        ) = Event(
            id = id,
            type = type,
            payload = payload,
            typeInfo = typeInfoT()
        )
    }
}
so maybe there's a different way to solve this altogether
c
don't I need to do that for each data class manually then?
Yes, but you can create helpers:
Copy code
class Typed<T> internal constructor(
    val value: T,
    val type: TypeInfo,
) {
    override fun equals(other: Any?) = other is Typed<*> && other.value == value
    override fun hashCode(): Int = value.hashCode()
    override fun toString() = "$value {$type}"
}

inline fun <reified T> Typed(value: T): Typed<T> =
    Typed(value, typeInfo<T>())
and then
Copy code
class Event<out T : Any> @PublishedApi internal constructor(
    val id: String,
    val type: String,
    val payload: Typed<T>,
)
No need for the inline constructor for this type, since you pushed the inline to the constructor of
Typed
, which happens earlier
So construction is now
Copy code
val e = Event(
    …,
    Typed(whatever)
)
and the
inline
is only here, instead of the whole chain
b
ah, maybe that can be exported; I think it complained about not being able to inline companion methods
in Java I was able to use the private Class<T> type; workaround I think
c
not being able to inline companion methods
🤔 I don't see why
TypedInfo
has much more information than
Class<T>
For example
List<String>::class
and
List<Int>::class
both return
Class<List<*>>
, whereas
TypeInfo
knows what the type parameter is. That's very important for serialization
b
Declaration of such kind (inline function with reified type parameters) cannot be exported to JavaScript.
pretty obvious I think since there's no JS compiler that can inline it
I think I'd need a create function for each instance and slap @JsExport onto those
c
It's the same reason why almost all KtMongo operators are
inline
b
which I guess is the same as a parent constructor call sort of?
c
yeah
for example
(ignore the internal Kotlin annotation, it's for something else)
K 1
pretty obvious I think since there's no JS compiler that can inline it
yes