KCallable is the Kotlin reflection superinterface shared by functions and properties. Its generic call method invokes a reflected function or a property's getter with runtime arguments. section 12.2.1
Calling a reflected function
fun foo(x: Int) = println(x) val kFunction = ::foo kFunction.call(42) // 42 The ::foo reference produces a KFunction, which implements KCallable. call accepts arguments through vararg, so an incorrect argument count causes a runtime IllegalArgumentException. section 12.2.1
Choose the more specific callable interface when the function signature is known:
- KFunction1<Int, Unit> exposes one parameter and a Unit return type; invoke accepts exactly one Int, and the function reference can also be called directly. section 12.2.1
- KCallable.call provides a generic reflective path, while KFunction interfaces provide fixed-arity invoke operations with parameter and return-type information. section 12.2.1
- KCallable.callBy can invoke methods while supplying default parameter values. section 12.3
Properties are callable too
var counter = 0 val kProperty = ::counter kProperty.setter.call(21) println(kProperty.get()) // 21 A reflected mutable property exposes getter and setter operations. Member properties can likewise retrieve a value by receiving the object, as in Person::age.get(person). section 12.3
KFunction and KProperty extend KCallable, while arity-specific function and property interfaces add typed invocation or value access. This hierarchy lets reflection enumerate declarations and invoke them dynamically at runtime. section 12.3
