Just how bad is resolution (`KSTypeReference.resol...
# ksp
e
Just how bad is resolution (
KSTypeReference.resolve()
)? in terms of performance. I’m mildly curious because the docs say to avoid it unless necessary but it might affect correctness if you don’t resolve 🤔 For example: I have these functions trying to check if a class (
KSClassDeclaration
) extends a particular abstract class (with fqcn
KSName
)
Copy code
//
classDeclaration.superTypes.any { it.isClassWithName(superClassName) }

//
private fun KSTypeReference.isClassWithName(className: KSName): Boolean =
  element?.isClassWithName(className) == true 
  // Wondering if its necessary to resolve type reference like below or is above name comparison sufficient 
  // && resolve() == resolver.getClassDeclarationByName(className)?.asType()

private fun KSReferenceElement.isClassWithName(className: KSName): Boolean {
  return when (val ref = this) {
     
    is KSParenthesizedReference -> ref.element.isClassWithName(className)
    
    is KSClassifierReference -> {
      val name = ref.referencedName(). // This is a simple text comparison and can break depending on what code the end user writes :(
      className.asString().commonSuffixWith(name) == name
    }
    
    else -> false
  }
}
j
When you want to get the type you have to resolve, the comment on the performance is to give you an insight to not rely on resolution excessively, but not to advice against resolution.
👍🏼 1
e
Thanks @Jiaxiang. Thats insightful