> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin In

The `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:**

```kotlin theme={"dark"}
a in b    // Translates to: b.contains(a)
a !in b   // Translates to: !b.contains(a)
```

**Implementation requirements:**
For an object to support the `in` operator for membership, its class (or an extension function) must define a function named `contains` that:

1. Is prefixed with the `operator` modifier.
2. Accepts a parameter of the left-hand operand's type.
3. Returns a `Boolean`.

```kotlin theme={"dark"}
class CustomContainer {
    operator fun contains(element: String): Boolean {
        // Implementation logic
        return true
    }
}
```

## 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:**

```kotlin theme={"dark"}
when (subject) {
    in collection -> println("In collection") // Executes if collection.contains(subject) is true
    !in range -> println("Not in range")      // Executes if !range.contains(subject) is true
}
```

## 3. Iteration (Control Flow)

When used within a `for` 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:**

```kotlin theme={"dark"}
for (item in collection) { /* ... */ }
```

Translates under the hood to a `while` loop utilizing an iterator:

```kotlin theme={"dark"}
val iterator = collection.iterator()
while (iterator.hasNext()) {
    val item = iterator.next()
    // ...
}
```

**Implementation requirements:**
To support `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:

1. `operator fun next(): T`
2. `operator fun hasNext(): Boolean`

## 4. Generics (Contravariance Modifier)

While not an operator in this context, the `in` 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.

```kotlin theme={"dark"}
interface Consumer<in T> {
    fun consume(item: T)
    // fun produce(): T // Compilation error: T is declared as 'in'
}
```

**Use-site variance (Type Projections):**
Applied to a type argument at the point of use. If a generic type is invariant at its declaration, applying `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.

```kotlin theme={"dark"}
// Array is invariant at the declaration site: class Array<T>
fun addAll(items: Array<in String>) {
    items[0] = "String" // Allowed: Consuming String
    // val item: String = items[0] // Compilation error: Projected as 'in', returns Any?
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
