Hi guys, is it possible with KSP to get the defaul...
# ksp
r
Hi guys, is it possible with KSP to get the default value of a function parameter which function is annotated? I am able to check if there is a default value for that parameter (using
KSValueParameter.hasDefault
), but not what that value is..
e
Looks like it's not possible right now. May be worth opening an issue
z
I don't believe it's possible because this information isn't stored in metadata other than that boolean
it would be quite hard to do so considering it can be arbitrary expressions
r
Imagine a scenario where you want to call the annotated function from the generated code. And there are certain runtime conditions that make you decide wether to call the function “overriding” the arguments OR call it with the default values. My issue is that I would need to generate code for all possibilities: lets say the function has 2 arguments with default values, then I would need 4 decision branches: 1. I need to pass both arguments 2. Both arguments should use the default values (I can’t pass any arguments) 3. I want to pass argument 1 but not argument 2 4. I want to pass argument 2 but not argument 1. And this is even worse the more arguments with default values there are. As far as I know Kotlin does not have any feature where I can explicitly say:
function(arg1 = [DEFAULT_VALUE], arg2 = "foo")
right? I need to explicitly pass arg1 or don’t pass it in order for the default value to be used. Now I can’t talk to the possibility of the feature, but doesn’t seem to crazy and I would guess It would be useful in many cases.
z
your only solution is to reflectively (or generate bytecode to) invoke the defaults function/constructor with the right mask value. Moshi does this, for example
👍 2
r
Alright, cool 🙂 I’ll take a look. Thanks for the feedback!
👍 1
352 Views