What is the contract in Kotlin?
Answer
Kotlin contracts are a mechanism for library functions to communicate additional guarantees to the compiler about their behavior, enabling smarter analysis and smart casts. For example, a contract can tell the compiler that a lambda will be called exactly once, allowing the compiler to recognize that variables initialized inside the lambda are definitely initialized afterward. Or a contract can tell the compiler that if a function returns normally, a certain condition is true. Contracts are declared with the contract { } DSL block inside the function body. They are experimental/stable features used in the standard library (e.g., run, with, let use them internally).
Previous
What is an object expression (anonymous object) in Kotlin?
Next
What is Flow.catch in Kotlin?