David Kubecka
05/24/2024, 5:17 PMinterface Job
// execute a job instance of [T] with [data]
fun <T : Job> executeJob(data: Map<String, Any?>) {
TODO()
}
class MyJob : Job {
// the job framework sets these variables from the data map
lateinit var id: UUID
lateinit var name: String
fun doJob() {
TODO()
}
}
So at the start, something calls executeJob<MyJob>(mapOf("id" to someId, "name" to someName))
.
The question is how to make this typesafe, i.e. how to best associate with each Job
the data structure it expects. The most obvious thing is to simply wrap the data inside a data class, e.g. MyJobData
, and then for each job type define a specific execute function, e.g. fun executeMyJob(data: MyJobData)
. I wonder, though, whether there's a more elegant/clever way how to express this MyJob
-MyJobData
relation in the type system.Youssef Shoaib [MOD]
05/24/2024, 5:59 PMMyJob
itself take those as parameters? and then executeJob
can simply take an instance of T
David Kubecka
05/24/2024, 6:46 PMYoussef Shoaib [MOD]
05/24/2024, 6:58 PMinterface Job<Data> {
var data: Data
}
// execute a job instance of [T] with [data]
fun <T : Job<Data>, Data> executeJob(data: Data) {
TODO()
}
class MyJob : Job<MyJobData> {
override var data: MyJobData
fun doJob() {
TODO()
}
}
data class MyJobData(...)
David Kubecka
05/24/2024, 7:38 PMexecuteJob<MyJob, _>(myJobData)
David Kubecka
05/24/2024, 7:42 PM