liveBook · Manning

Delegated property

As explained in Kotlin in Action, 2E

A delegated property is a Kotlin property whose read or write behavior is provided by a separate delegate object.

A delegated property keeps ordinary property syntax while moving storage or accessor behavior to a helper object. This removes repeated getter and setter logic when properties need lazy loading, validation, change notifications, or storage outside a backing field. section 9.5 section 9.5.4

var p: Type by Delegate()

The expression after `by` creates or supplies the delegate that handles property access. section 9.5.1

For `var p: Type by Delegate()`, Kotlin evaluates the delegate expression and stores the result in a hidden helper property. Reads call `getValue`; writes call `setValue`. A `val` needs only `getValue`. Delegate functions are marked `operator` and may be members or extensions; `provideDelegate` can validate or customize delegate creation. section 9.5.1 section 9.5.4

Lazy initialization without manual state

Use `val emails by lazy { loadEmails(this) }` instead of a nullable backing property plus a custom getter. The lambda initializes the value on first access. `lazy` is thread safe by default, with options for different locking behavior or no synchronization when multithreading is irrelevant. section 9.5.2

Observable mutable properties

A delegate can hold a value and notify observers whenever it changes. `var age by ObservableProperty(age, this)` and `var salary by ObservableProperty(salary, this)` replace duplicated setter code. `Delegates.observable` supplies a standard-library alternative that receives the property, old value, and new value. section 9.5.3 section 9.5.3

Delegates also support framework-level property behavior:

  • A database column can act as a delegate: reading `user.age` retrieves an entity value, and writing it updates the mapped value and marks the entity dirty. section 9.5.6
  • Any map can serve as a property delegate for objects with variable sets of attributes. section 9.6

Delegation changes implementation, not use: clients still read and assign properties normally. It centralizes logic for how values are stored, initialized, accessed, and modified while preserving concise Kotlin APIs. section 9.5.4 section 9.6

How Kotlin in Action, 2E covers it

How deep it goes
Explains syntax, compiler-generated accessors, delegate conventions, custom implementations, and framework use.
Examples
Demonstrates lazy email loading, observable Person properties, map delegation, and database-backed User attributes.
What it assumes
Builds on Kotlin properties, accessors, operator functions, backing fields, and KProperty for custom delegates.
What you can do after
Build it from scratch with getValue and setValue delegates, or apply it through lazy and Delegates.observable.
How it connects
Connects delegation with operator overloading, lazy initialization, backing fields, observers, KProperty, maps, and database entities.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.