Bernhard
07/22/2026, 7:34 AMinterface 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)
}
}
}CLOVIS
07/22/2026, 7:45 AMTypeInfo<T> in Event next to payload, so the inline is at the construction of the event, and then usage doesn't have to beBernhard
07/22/2026, 8:07 AMval id: String,
val type: String,
val payload: T,
val typeInfo: TypeInfo,
?Bernhard
07/22/2026, 8:08 AMCLOVIS
07/22/2026, 8:15 AMthat's what I've got right nowIs it? That's not in your example
Bernhard
07/22/2026, 8:17 AMBernhard
07/22/2026, 8:17 AMBernhard
07/22/2026, 8:19 AMclass 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()
)
}
}Bernhard
07/22/2026, 8:19 AMCLOVIS
07/22/2026, 8:20 AMdon't I need to do that for each data class manually then?Yes, but you can create helpers:
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
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 earlierCLOVIS
07/22/2026, 8:21 AMval e = Event(
…,
Typed(whatever)
)
and the inline is only here, instead of the whole chainBernhard
07/22/2026, 8:21 AMBernhard
07/22/2026, 8:22 AMCLOVIS
07/22/2026, 8:22 AMnot being able to inline companion methods🤔 I don't see why
CLOVIS
07/22/2026, 8:23 AMTypedInfo 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 serializationBernhard
07/22/2026, 8:23 AMDeclaration of such kind (inline function with reified type parameters) cannot be exported to JavaScript.
Bernhard
07/22/2026, 8:25 AMBernhard
07/22/2026, 8:26 AMCLOVIS
07/22/2026, 8:26 AMinlineBernhard
07/22/2026, 8:27 AMCLOVIS
07/22/2026, 8:27 AMCLOVIS
07/22/2026, 8:27 AMCLOVIS
07/22/2026, 8:27 AMCLOVIS
07/22/2026, 8:28 AMpretty obvious I think since there's no JS compiler that can inline ityes