https://kotlinlang.org logo
#ksp
Title
# ksp
j

Jake Woods

06/29/2022, 7:01 AM
Is there any easy way in KSP to get the closest common ancestor of two
KSType
? I’m doing the resolution with a hand-written algorithm right now and it feels wrong
For example, say I have a
KSType
of
Either<Nothing, Int>
and another of
Either<Throwable, Nothing>
, I want to infer that their common ancestor is
Either<Throwable, Int>
j

Jiaxiang

06/29/2022, 8:43 PM
It doesn’t really work as a common ancestor, you need to have variance in order to make it a common ancestor, for example you need to have
Either<out T, out P>
declared. Even after that, you still need to manually write a least common ancestor algorithm to determine the common ancestor you are looking for, and you also need to deal it with recursively as the type arguments can still be generics.
j

Jake Woods

06/29/2022, 11:31 PM
In this example Either is defined as
Either<out E, out T>
so I think the ancestor exists. I guess I was hoping KSP already had a least common ancestor implementation but it sounds like I'll need to roll my own 🙂
j

Jiaxiang

06/29/2022, 11:32 PM
ah yes, you need to implement it yourself. This is not a common enough use case to be included in public API.
👍 1
3 Views