Kotlin Reflection and Default Values

Hey folks,

Is it possible using Kotlin’s reflection, or some other mechanism, to determine the default value for a function parameter? IntelliJ IDEA is able to pull this information to provide code completion tips showing the default value when showing Parameter Info, but I wasn’t able figure out how they achieve this. I found this which says, at least as of 2013, it wasn’t possible, but that was a while back. (I am mainly hoping to pull the value for documentation purposes, similar to how Parameter Info does.)

Thanks!

2 Likes

The default value of a method parameter is an arbitrary expression which can only be represented as a chunk of bytecode; there is no other representation that can be used in reflection. Parameter Info retrieves default parameter values by parsing source code.

2 Likes

Thank you @yole! I’ll look into pursuing the same approach as Parameter Info, then.