Skip to main content

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.

The !in operator is a negated membership operator used to evaluate whether a specific element is absent from a collection, range, or any object that defines a contains operator function. It serves as the syntactic inverse of the in operator. At compile time, the Kotlin compiler desugars the !in expression into a negated method invocation of contains() on the right-hand operand.
val a = 5
val b = listOf(1, 2, 3)

// Syntactic expression
val isAbsent = a !in b

// Desugared JVM equivalent
val isAbsentDesugared = !b.contains(a)

Operator Overloading

Because !in relies on the contains convention, it does not have its own dedicated operator function. To enable !in for a custom type, you must implement the contains function and mark it with the operator modifier. The function must accept the left-hand operand’s type and return a Boolean.
class BoundingBox(val min: Int, val max: Int) {
    // Enables both 'in' and '!in'
    operator fun contains(point: Int): Boolean {
        return point in min..max
    }
}

val box = BoundingBox(0, 100)
val isOutside = 150 !in box // Evaluates to !box.contains(150)

Type Resolution and Extension Functions

The right-hand operand is not strictly required to be a collection. The Kotlin compiler resolves the contains method either as a member function or as an extension function. For example, the standard library provides extension functions for Iterable<T>, Array<T>, and CharSequence, allowing !in to be evaluated against diverse data structures.
// Resolves to CharSequence.contains(Char)
val notAVowel = 'z' !in "aeiou" 

// Resolves to Iterable<T>.contains(T)
val element = 42
val objectList = listOf(1, 2, 3)
val notFound = element !in objectList 

Syntax in Control Flow

Beyond standard boolean expressions, !in is a recognized branch condition within when expressions. In this context, the compiler implicitly applies the !in operator against the subject of the when block.
fun handleOutliers() { /* ... */ }
fun processStandard() { /* ... */ }

val subject = 150

when (subject) {
    !in 0..100 -> handleOutliers() // Desugars to: !(0..100).contains(subject)
    else -> processStandard()
}
Master Kotlin with Deep Grasping Methodology!Learn More