Are there any library can resolve the default valu...
# kapt
i
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
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
Sorry, it's a typo. I mean parameters.😅
t
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
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
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
Ah... 😣 Thank you for your reply. Parsing the code for documentation is a huge works.
t
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
emmm thank you. I will have a try.