I hope it's not inappropriate to mention this here...
# ksp
d
I hope it's not inappropriate to mention this here, but I feel like the https://kotlinlang.org/docs/ksp-examples.html page can be filled out with many more code recipes.
😢 1
j
It should be done better, if you feel you have good suggestions for examples, feel free to submit PR for document and I will review it.
d
I can't submit PRs because I don't know how to do yet what I want to do 🙂 The two things I was hoping to find examples for: • How to test if a class is a subclass of another class (potentially many levels down) • How to pull generic values out a base class (so for
A : B<String, Int>
, given
A
, how do I know that it implements
B
with generic types specified as
String
and
Int
I'm sure getting generic type information out of other things, like a top level function or property, could be useful. If no one else agrees with me, then maybe it's not necessary. But I was also hoping that others might chime in with their own interesting cases.
An example of using
KSClassDeclaration.asStarProjectedType()
might be interesting.
👍 1
j
subclass checking an easier way you can try making it star projected type and then check for subtyping relations. Same goes to the type variable part, if you don’t care about type variable you can simply check with star projected types, but in my opinion type variables are generally important, you might still want to plug in the actual types and then check.
👍 1
d
Thanks
I'm mostly exploring KSP by using code completion, which gives me a lot of options which I'm not too familiar with yet, so I go down the wrong rabbit holes repeatedly
j
it is subtle that I always feel it is better to do a concept illustrations than doing code examples, but I found no good ways to do that in either directions
d
I also see we're not supposed to use
resolve
too much, but I feel like most times I have to? So when I see it used in the examples I'm happy, it means I'm not crazy to use it. But I do worry that I might start overusing
resolve
when there were non-resolve ways to handle it.
Sure, illustrations would be incredible, but without them, I think I could build up a good mental model just by looking at a bunch of examples.
j
resolve
is recommended to avoid, but when you have to, just feel no pressure to use it.
d
Well, I think I have to resolve when walking up class super types, but I'm not sure if there's another way.
So more like maybe there are ways to avoid it that I'm not familiar with.
j
There shouldn’t be many scenarios where
resolve
can be replaced with a non resolve solution, the scenario you just mentioned happen to be one but I don’t think it is worth it to do given the potential deep recursive stack we have to go through, if I were you and if I were not in a situation where I must squeeze all performance gains, I would just go with a resolved situation.
👍 1
d
I don't actual expect anyone to do this, but it could be instructive to flatten all KSP methods and properties (e.g. all methods from
KSClassDeclaration
,
KSTypeReference
etc.), put them into a spreadsheet (so first column has
KSClassDeclaration::classKind
,
KSClassDeclaration::asStarProjectedType
,
KSTypeReference::element
, etc.), and then put a check in the second column for what shows up in the examples. I don't think examples have to be exhaustive by any means, but maybe this will highlight some of the methods and properties that you think should be served by an example, at which point you can try to create a realistic use-case that demonstrates them.
That's my feedback as a beginner, at least. Maybe that opinion will change the more I use KSP or find example code or figure things out. And maybe others disagree with me!
👍 1