skip to content

What is KCallable in Kotlin reflection, and how do KFunction and KProperty relate to it?

level: juniorimportance: should knowfreq 35%

answer

  1. KCallable = root of callable reflection
  2. KFunction + KProperty are its subtypes
  3. name / parameters / returnType / visibility
  4. call() and callBy() live on KCallable
  5. needs kotlin-reflect artifact

basics

~10 s

KCallable is the common reflection type for things you can call or read: functions and properties. KFunction represents a function, and KProperty represents a property; both are kinds of KCallable.

solid answer

~30 s

KCallable<R> is the supertype in kotlin-reflect for any callable member or top-level declaration, where R is its return type. Its two main subtypes are KFunction<R> (functions, constructors, lambdas) and KProperty<R> (val/var properties). KCallable exposes shared metadata: name, parameters (List<KParameter>), returnType (a KType), visibility, typeParameters, plus annotations and flags like isAbstract, isFinal, isSuspend. You obtain KCallables from a KClass via members, functions, or declaredMemberProperties, or via callable references. The key idea: both calling a function and reading a property are 'invoking something with arguments', so they share the call(vararg args)/callBy(map) API on KCallable. Requires the kotlin-reflect artifact at runtime.

code

kotlin · 6 lines
kotlin
import kotlin.reflect.KCallable

class Box(val size: Int) { fun open() = size }

val callables: Collection<KCallable<*>> = Box::class.members
callables.forEach { println("${it.name}: returns ${it.returnType}") }

go deeper

for a junior

Knows KCallable is the umbrella for functions and properties and can name KFunction/KProperty.

for a middle

Can list shared members (name, parameters, returnType, visibility) and knows call/callBy exist.

for a senior

Explains the receiver-count split of KProperty and the kotlin-reflect runtime dependency, and that parameters includes receivers.

for a principal

Discusses why unifying callables benefits framework design and the performance/cost tradeoffs of relying on it.

## What is `KCallable`? `KCallable<out R>` is the root interface in the Kotlin reflection API (`kotlin.reflect`) for anything that can be **called** — functions and properties alike. The type parameter `R` is the **return type** of the callable (a function's return value, or a property's value type). Reflection means inspecting and invoking code structure **at runtime** rather than compile time. To use the full Kotlin reflection API you must add the **`kotlin-reflect`** dependency; without it, most of these members throw `KotlinReflectionNotSupportedError`. ## The `KCallable` hierarchy - **`KFunction<R>`** — represents a function, constructor, or lambda. Its `R` is the function's return type. - **`KProperty<R>`** — represents a property (`val`/`var`). Its `R` is the property's value type. It has subtypes by receiver count: `KProperty0` (no receiver), `KProperty1` (one), `KProperty2` (two), and **`KMutableProperty`** variants for `var`s. ## Shared members on `KCallable` Because every callable is conceptually "invoke with some arguments", `KCallable` exposes: - `name: String` — the declared name. - `parameters: List<KParameter>` — formal parameters, **including** any `INSTANCE` (dispatch) and `EXTENSION_RECEIVER` parameters. - `returnType: KType` — the return/value type. - `typeParameters: List<KTypeParameter>` — generic type params. - `visibility: KVisibility?` — `PUBLIC`, `PROTECTED`, `INTERNAL`, `PRIVATE`, or `null` if it cannot be represented. - Flags: `isAbstract`, `isFinal`, `isOpen`, `isSuspend`. - `call(vararg args: Any?): R` and `callBy(args: Map<KParameter, Any?>): R` to invoke it. ```kotlin import kotlin.reflect.full.declaredMemberProperties import kotlin.reflect.full.functions class User(val name: String) { fun greet(loud: Boolean): String = if (loud) "HI" else "hi" } val kClass = User::class val fns: Collection<KFunction<*>> = kClass.functions // KFunctions val props = kClass.declaredMemberProperties // KProperty1s fns.first { it.name == "greet" }.let { fn -> println(fn.name) // greet println(fn.returnType) // kotlin.String } ``` ## Why it matters Unifying functions and properties under one type lets generic tooling (DI, serializers, validators) treat "a thing I can read or invoke" uniformly.

  • What does the type parameter R on KCallable<R> represent?
    The return type for a function, or the value type for a property.
  • Name the two primary subtypes of KCallable.
    KFunction and KProperty.

KCallable is like a universal remote: whether the target is a TV (function) or a lamp (property), there's one 'press button' interface.

saying these in an interview costs you the question

  • Thinking KCallable only covers functions, not properties
  • Believing reflection works without the kotlin-reflect dependency
  • Confusing KCallable (member) with KClass (type)
  • Saying R is always the function's first parameter type

context