TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
in keyword in Kotlin functions primarily as a statically resolved operator used for membership evaluation and iteration, and secondarily as a variance modifier (both declaration-site and use-site) in generics. At compile time, the Kotlin compiler desugars the in operator into specific underlying function calls based on its syntactic context and the static types of the operands.
1. Membership Evaluation (Containment)
When used as a binary operator in a boolean expression,in acts as syntactic sugar for the contains function. Operator resolution is strictly statically resolved at compile time based on the static types of the operands. If the contains function is provided via an extension function, the method invocation is statically dispatched.
Desugaring mechanism:
in operator for membership, its class (or an extension function) must define a function named contains that:
- Is prefixed with the
operatormodifier. - Accepts a parameter of the left-hand operand’s type.
- Returns a
Boolean.
2. Branch Conditions in when Expressions
The in and !in keywords function as distinct syntactic constructs when defining branch conditions within a when expression. The compiler evaluates whether the subject of the when expression is a member of the collection or range specified after the in keyword. This relies on the same statically resolved contains contract used in standard membership evaluation.
Syntax:
3. Iteration (Control Flow)
When used within afor loop declaration, the in operator dictates sequential iteration. It does not invoke contains; instead, it relies on the iterator contract. Like membership evaluation, the resolution of the required functions is statically resolved based on the operand’s static type.
Desugaring mechanism:
while loop utilizing an iterator:
in for iteration, the right-hand operand must provide an iterator() function (member or extension) marked with the operator modifier. The object returned by iterator() must subsequently provide:
operator fun next(): Toperator fun hasNext(): Boolean
4. Generics (Contravariance Modifier)
While not an operator in this context, thein keyword is utilized in generics to denote contravariance. It restricts a type parameter to be consumed (passed as an argument into functions) and prevents it from being produced (returned from functions). This is applied at two levels:
Declaration-site variance:
Applied directly to the type parameter in the class or interface declaration. This instructs the compiler to allow safe subtyping globally for that type, meaning a Consumer<Number> can be safely assigned to a Consumer<Int>, reversing the standard subtyping relationship.
in at the use-site projects it as contravariant. This restricts the operations available on the projected instance to only those that consume the type parameter, enabling safe subtyping for that specific usage.
Master Kotlin with Deep Grasping Methodology!Learn More





