I'm using a job framework and I'm chaining several jobs and passing data between them. Currently, the job definitions look basically like this
interface 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.