https://kotlinlang.org logo
#kapt
Title
# kapt
i

iseki

04/25/2021, 10:17 AM
Are there any library can resolve the default value of a parameter? Thank you. I know it might be an AST or something. I only need it for documents.
t

TwoClocks

04/26/2021, 7:16 AM
I'm not sure what you mean by "the default value of a function"? If you mean the return type, or the parameters, you can get those via reflection. Do you mean the default value of a function parameter? That might be a little tricky.
i

iseki

04/26/2021, 7:18 AM
Sorry, it's a typo. I mean parameters.😅
t

TwoClocks

04/26/2021, 7:20 AM
I think the compiler erases the default param, so at runtime it's all just normal function calls.
or maybe you can use
callBy()
to call it w/ default params (maybe, not sure), but I don't know how to query them.
i

iseki

04/26/2021, 7:21 AM
Yes it is. But are there any information stored in meta? For example, IDEA can fetch the default value of parameter to quick docs.
So I want to create something work like it. Use the default value fill the document.
t

TwoClocks

04/26/2021, 7:23 AM
IDEA is parsing the source and/or AST... both of which are gone by runtime.
you could try a compiler plugin. That would likely be able to dig it out.
i

iseki

04/26/2021, 7:25 AM
Ah... 😣 Thank you for your reply. Parsing the code for documentation is a huge works.
t

TwoClocks

04/26/2021, 7:25 AM
but that's likely a PIA. You could just write a regex to pull them out of the source, if that's all you care about.
it might be a little janky, but it'd work. You could upgrade to using a actual parser for bonus points... but that's moving along the PIA scale a bit. Depends on how much you care about making it generic
is this just for your own project, or is this supposed to be a generic solution for other people to use?
here is a source parser : https://github.com/kotlinx/ast. I'd see if it supports default params.
i

iseki

04/26/2021, 8:25 AM
emmm thank you. I will have a try.
2 Views