> ## 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 Type Check

The `is` operator evaluates whether an object is an instance of a specified type at runtime. It performs runtime type identification (RTTI) and returns a `Boolean`. Its negated counterpart, `!is`, returns `true` if the object is not an instance of the specified type.

```kotlin theme={"dark"}
expression is Type
expression !is Type
```

## Compiler Mechanics

**Smart Casting**
When an `is` check evaluates to `true`, the Kotlin compiler performs data-flow analysis. If the compiler can guarantee that the variable will not be modified between the check and its subsequent usage, it automatically applies a **smart cast**. Within the scope where the condition holds true, the variable is treated as the target type, eliminating the need for the explicit cast operator (`as`).

```kotlin theme={"dark"}
fun process(obj: Any) {
    if (obj is String) {
        // obj is smart-cast to String; String properties are accessible
        print(obj.length) 
    }
    
    if (obj !is String) return
    // obj is smart-cast to String in the remaining scope
    print(obj.length)
}
```

**`when` Expressions**
The `is` operator is natively integrated into `when` expressions as a branch condition. This provides an idiomatic mechanism for multi-way type checking and smart casting without requiring explicit `if-else` chains.

```kotlin theme={"dark"}
fun evaluate(obj: Any) = when (obj) {
    is String -> print("String length: ${obj.length}") // Smart-cast to String
    is Int -> print("Integer value: $obj")             // Smart-cast to Int
    !is Boolean -> print("Not a boolean")
    else -> print("Unknown type")
}
```

**Nullability Resolution**
The `is` operator strictly adheres to Kotlin's type system regarding nullability.

* Checking against a non-nullable type (`is Type`) implicitly acts as a null check. If the operand is `null`, the expression evaluates to `false`.
* Checking against a nullable type (`is Type?`) evaluates to `true` if the operand is `null`, because `null` is a valid instance of `Type?`.

```kotlin theme={"dark"}
val obj: Any? = null

val checkOne = obj is String  // Evaluates to false
val checkTwo = obj is String? // Evaluates to true
```

## JVM Type Erasure Limitations

Because Kotlin compiles to the JVM (in its primary target), generic type arguments are generally erased at runtime. Consequently, the `is` operator cannot evaluate parameterized types with specific type arguments by default.

To perform type checks on generic collections or classes, you must use **star-projected types** (`*`), which verify the base class but not the generic payload.

```kotlin theme={"dark"}
val list: Any = listOf(1, 2, 3)

// COMPILER ERROR: Cannot check for instance of erased type: List<Int>
val invalidCheck = list is List<Int> 

// VALID: Checks if the object implements the List interface
val validCheck = list is List<*> 
```

**Exceptions to Type Erasure**
There are three specific scenarios where the compiler permits the `is` operator to evaluate exact generic type arguments:

1. **Reified Type Parameters:** When using `inline` functions with `reified` type parameters, the compiler inlines the actual type at the call site, preserving the type information at runtime.
2. **Arrays:** Arrays in Kotlin are reified on the JVM. They retain their component types at runtime, allowing direct type checks against specific array types.
3. **Statically Known Type Arguments:** If the compiler's static type checking already guarantees the generic type argument based on the variable's declaration, it permits an `is` check on the base class.

```kotlin theme={"dark"}
// 1. Reified Type Parameters
inline fun <reified T> checkType(obj: Any): Boolean {
    return obj is T // VALID: T is reified
}

// 2. Arrays
val arr: Any = arrayOf(1, 2, 3)
val isIntArray = arr is Array<Int> // VALID: Arrays retain component types

// 3. Statically Known Type Arguments
val collection: Collection<Int> = setOf(1, 2, 3)
val isList = collection is List<Int> // VALID: <Int> is statically known from Collection<Int>
```

<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>
