Is there a way to use `where` clause when defining...
# announcements
l
Is there a way to use
where
clause when defining
typealiases
?
l
What do you want to accomplish?
Maybe not with that exact ordering, but tell us an example of what you want
l
I’m exploring interface composition
Copy code
interface 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
Copy code
typealias Data = HasId & HasName
z
m
You can define a interface instead of typealias
Copy code
interface 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-kt
1
l
@Zac Sweers I do want that, but for another use case. For this case, I need a sort of a sum type that allows defining multiple interface constraints.
Copy code
data 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).