Kotlin Type-Safe Builders

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


A type-safe builder is a concept, rather than a language feature, so it is not strictly formalized.

A typical structure of a type-safe builder

A single builder function usually consists of 3 steps:

  1. Create an object.
  2. Execute lambda to initialize the object.
  3. Add the object to structure or return it.

Type-safe builders in Kotlin libraries

The concept of type-safe builders is widely used in some Kotlin libraries and frameworks, eg.:

  • Anko
  • Wasabi
  • Ktor
  • Spec

Got any Kotlin Question?