skip to content

KCallable, KFunction & KProperty

KCallable is the shared abstraction over functions and properties, with KFunction and the KProperty family underneath, exposing parameters, return types, and visibility. Enumerating declared properties is the entry point for most reflective mapping code.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

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

open as a page

Explain the difference between KProperty0, KProperty1, and KProperty2, and where KMutableProperty fits in.

level: middleimportance: should knowfreq 40%

basics

~10 s

The number means how many receivers you must pass to read the property. KProperty0 needs none, KProperty1 needs one (the object), KProperty2 needs two. KMutableProperty versions also let you write the value.

open as a page

Compare members, functions, and declaredMemberProperties on a KClass. What does each return and how do they differ regarding inheritance?

level: middleimportance: should knowfreq 38%

basics

~10 s

members lists all callable members (functions and properties), including inherited ones. functions lists functions including inherited. declaredMemberProperties lists only properties declared in that class, not inherited ones.

open as a page

How do you inspect a KFunction's parameters, returnType, and visibility, and what do the KParameter.kind values mean?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Read function.parameters to get a list of KParameter (each has name, type, index, and a kind), function.returnType for the result type, and function.visibility for public/internal/etc. The kind tells you if a parameter is the receiver or a normal value.

open as a page

You need to build a generic 'object to map' serializer using declaredMemberProperties. How do you read each property's value and type generically, and what pitfalls (visibility, isAccessible, nullability) must you handle?

level: seniorimportance: nice to knowfreq 22%

basics

~10 s

Loop over the class's declaredMemberProperties, call get(instance) on each to read the value, and use property.returnType for the type. Watch out for private properties (set isAccessible) and nullable values.

open as a page