!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.
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.
Type Resolution and Extension Functions
The right-hand operand is not strictly required to be a collection. The Kotlin compiler resolves thecontains 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.
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.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More





