Vera van Mondfrans
09/29/2020, 2:13 PMabstract class ESGenerics<IDType : DomainID, EntityType : Entity>(
private val elasticsearchClient: RestHighLevelClient,
private val objectMapper: ObjectMapper
) {
// ...
}
All Entity classes has an ID member that should always extend DomainID, so I made this:
interface Entity {
val id: DomainID
}
These functions need EntityType and IDType as types for arguments. Now I feel like I shouldn’t have to pass the IDType to ESGenerics. After all, the EntityType always has an ID field with the correct type. But I can’t figure out how to access the type of EntityType’s ID member in a function declaration. I’d like to rewrite it from:
fun getByIDs(ids: List<IDType>): List<EntityType> {
To:
fun getByIDs(ids: List<EntityType::id>): List<EntityType> {
How can I do that?Nir
09/29/2020, 2:18 PMIDType
is a type parameterVera van Mondfrans
09/29/2020, 2:18 PMNir
09/29/2020, 2:18 PMVera van Mondfrans
09/29/2020, 2:19 PMNir
09/29/2020, 2:19 PMNir
09/29/2020, 2:19 PMNir
09/29/2020, 2:20 PMESGenerics
no longer generic on IDType
, and only use DomainId
. is that what you're planning?Vera van Mondfrans
09/29/2020, 2:20 PMVera van Mondfrans
09/29/2020, 2:20 PMNir
09/29/2020, 2:21 PMVera van Mondfrans
09/29/2020, 2:21 PMNir
09/29/2020, 2:21 PMESGenerics
Vera van Mondfrans
09/29/2020, 2:21 PMVera van Mondfrans
09/29/2020, 2:23 PMVera van Mondfrans
09/29/2020, 2:24 PMEntityType["id"]
Nir
09/29/2020, 2:24 PMNir
09/29/2020, 2:25 PMVera van Mondfrans
09/29/2020, 2:25 PMVera van Mondfrans
09/29/2020, 2:25 PMNir
09/29/2020, 2:25 PMNir
09/29/2020, 2:26 PMVera van Mondfrans
09/29/2020, 2:26 PMVera van Mondfrans
09/29/2020, 2:26 PMVera van Mondfrans
09/29/2020, 2:26 PMVera van Mondfrans
09/29/2020, 2:26 PMVera van Mondfrans
09/29/2020, 2:26 PMNir
09/29/2020, 2:26 PMNir
09/29/2020, 2:27 PMVera van Mondfrans
09/29/2020, 2:28 PMVera van Mondfrans
09/29/2020, 2:28 PMNir
09/29/2020, 2:28 PMEntityType
, but you can only use the static types up to the guarantees of the constraint. In this case, the constraint is Entity
, so you'd have to use the base type, DomainID
Vera van Mondfrans
09/29/2020, 2:29 PMVera van Mondfrans
09/29/2020, 2:29 PMNir
09/29/2020, 2:29 PMfun getByIDs(ids: List<DomainId>)
Nir
09/29/2020, 2:30 PMVera van Mondfrans
09/29/2020, 2:30 PMNir
09/29/2020, 2:30 PMNir
09/29/2020, 2:31 PMEntityType
can only be used (at compile time) as an Entity
, and Entity
onlyknows DomainId
, nothing moreNir
09/29/2020, 2:32 PMEntity
interface itself genericVera van Mondfrans
09/29/2020, 2:32 PMNir
09/29/2020, 2:32 PMinterface Entity<IDType : DomainID> {
val id: IDType
}
Nir
09/29/2020, 2:33 PMNir
09/29/2020, 2:34 PMESGenerics
is still generic over IDType
Vera van Mondfrans
09/29/2020, 2:36 PMfun getByIDs(ids: List<EntityType::/*what do I do here*/>)
Vera van Mondfrans
09/29/2020, 2:36 PMNir
09/29/2020, 2:37 PMVera van Mondfrans
09/29/2020, 2:37 PMVera van Mondfrans
09/29/2020, 2:38 PMNir
09/29/2020, 2:38 PMVera van Mondfrans
09/29/2020, 2:38 PMNir
09/29/2020, 2:38 PMNir
09/29/2020, 2:39 PMNir
09/29/2020, 2:40 PMNir
09/29/2020, 2:40 PMVera van Mondfrans
09/29/2020, 2:41 PMNir
09/29/2020, 2:41 PMMap.Entry<K, V>
Nir
09/29/2020, 2:41 PMNir
09/29/2020, 2:41 PMMap
isn't a type at allNir
09/29/2020, 2:42 PMNir
09/29/2020, 2:42 PMMap
is something that takes two types, and gives you a new type.Nir
09/29/2020, 2:42 PMMap<K, V>.Entry
Nir
09/29/2020, 2:42 PMVera van Mondfrans
09/29/2020, 2:43 PMVera van Mondfrans
09/29/2020, 2:43 PMNir
09/29/2020, 2:43 PMVera van Mondfrans
09/29/2020, 2:43 PMNir
09/29/2020, 2:43 PMVera van Mondfrans
09/29/2020, 2:43 PMVera van Mondfrans
09/29/2020, 2:44 PMVera van Mondfrans
09/29/2020, 2:44 PMNir
09/29/2020, 2:45 PM