KCallable.callBy invokes a Kotlin function or constructor from a map of KParameter keys to argument values. Unlike call, it can use default parameter values when arguments are omitted, and map entries need not follow declaration order. section 12.2.5
Reflective construction from named data
A deserializer can read a constructor’s parameter names from JSON, resolve each name to a KParameter, convert the value to the parameter’s KType, and invoke the constructor with constructor.callBy(arguments). A missing parameter uses its default value when possible, so callers need not include defaulted properties in the JSON. section 12.2.5
Using callBy reliably requires three checks:
- Use KParameter keys rather than positional argument indexes; callBy accepts a Map<KParameter, Any?>. section 12.2.5
- Match every supplied value to the constructor parameter type. Incorrect values cause IllegalArgumentException at run time, especially when converting JSON numbers to Int, Long, or Double. section 12.2.5
- Use KParameter.type to select or apply a suitable ValueSerializer, including custom serializers where configured. section 12.2.5
interface KCallable<out R> {
fun callBy(args: Map<KParameter, Any?>): R
// ...
}
The core signature makes the contract explicit: callBy receives a parameter-to-value map and returns the callable’s result. section 12.2.5
