laimiux
07/15/2020, 1:58 AMwhere
clause when defining typealiases
?LeoColman
07/15/2020, 2:38 AMLeoColman
07/15/2020, 2:38 AMlaimiux
07/15/2020, 5:26 AMinterface HasId {
val id: String
}
interface HasName {
val name: String
}
fun <T> doSomething(data: T): Unit where T : HasId, T : HasName {
// whatever
}
This gets cumbersome when you have multiple functions. I would like to define typealias once and use it for multiple functions
typealias Data = HasId & HasName
Zac Sweers
07/15/2020, 6:24 AMMayank
07/15/2020, 6:52 AMinterface Data: HasId, HasName
or use reified for type checking as demonstrated in this blog, the required part being https://gist.github.com/yenerm/4ff23c907b56fe72f5b32e51a1ae0f8e#file-reified-ktlaimiux
07/15/2020, 3:54 PMdata class RootData() : HasId, HasName, HasProp1, HasProp2
The idea is to select only partial data without having to map from data type to another. So, I could select only HasId
and HasProp1
without having to ask for the whole RootData
object. The interface solution doesn’t work well because then RootData
would need to implement Data
interface which would couple data to all the places consuming it (I would prefer the data to not be aware where it is consumed).