Kotlin Download PDF
Table of contents

Kotlin language specification

Version 1.6-rfc+0.1

Marat Akhin

Mikhail Belyaev

Builder-style type inference

Some functions or parameters of functions in the standard library are annotated with the special @BuilderInference annotation, making calls to these functions eligible for the special kind of type inference: builder-style type inference. In order to allow builder-style inference for a function parameter, this parameter must hold the following properties:

In essence, the builder-style inference allows a receiver of an extension lambda parameter to be inferred from its usage in addition to standard type information sources. For a call to an eligible function with a lambda parameter LL the inference is performed as described above, but the type parameters of the receiver parameter of the lambda expression are postponed till the body of the lambda expression is proceeded. After the inference of statements inside the lambda body, these parameters are inferred using an additional type inference step:

Note: notable examples of builder-style inference-enabled functions are kotlin.sequence and kotlin.iterator. See standard library documentation for details.