Rodrigo Silva
02/02/2020, 8:21 PMfun<T: Any> testing( id: UUID, name: String) : T {
return T
}
If no, how do I return a T ?Czar
02/02/2020, 8:23 PMT
it needs to be reified, second of all, its type would be KClass<T>
, not <T>
. That said, maybe you mean something else, what do you want to achieve?Rodrigo Silva
02/02/2020, 8:28 PMfun process (id: UUID, commandStatus: CommandStatusEnum, message: Any): Command (or other class) {
return Command (id, status, message)
}
I don't want to create a function for each type of return, so I wanted to use GenericsDico
02/02/2020, 8:38 PMtypealias Process<T> = (id: UUID, name: String) -> T
Dico
02/02/2020, 8:39 PMDico
02/02/2020, 8:40 PMCzar
02/02/2020, 8:42 PMCzar
02/02/2020, 8:48 PMenum class CommandStatusEnum {
A, B, C
}
class Event(id: UUID, commandStatus: CommandStatusEnum, message: Any)
class Command(id: UUID, commandStatus: CommandStatusEnum, message: Any)
fun <T : Any> process(
id: UUID,
commandStatus: CommandStatusEnum,
message: Any,
creator: (id: UUID, commandStatus: CommandStatusEnum, message: Any) -> T
)T {
/* process stuff */
return creator(id, commandStatus, message)
}
// usage
process(UUID.randomUUID(), A, "", ::Event)
What makes this possible is that you know the type of object you want to receive, and resulting function is type-safe, whereas fun <T> process(id: UUID, commandStatus: CommandStatusEnum, message: Any): T
would require inlining, reification and use of reflection and would not be type-safe or refactoring friendly at all.