Kotlin Type-Safe Builders

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

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?