Using Operators

This Atom is Under Construction

In practice you rarely overload operators—usually only when you create your own library.

However, you regularly use operators, often even without noticing. For example, the Kotlin standard library defines numerous operators that improve your experience with collections. Let’s look at some familiar code from a new angle:

// UsingOperators/NewAngle.kt import atomictest.eq fun main(args: Array<String>) { val list = mutableListOf('a', 'b', 'c') list[1] eq 'b' // operator get() list.get(1) eq 'b' // Explicit call list[2] = 'x' // operator set() list.set(2, 'x') // Explicit call list[2] eq 'x' ('a' in list) eq true // operator contains() list.contains('a') eq true // Explicit call }

Accessing list elements using square brackets actually calls the overloaded get() and set() operators. in calls the overloaded contains().

End of sample. See for full early-access book.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.