Skip to content

Extensions and null safety ​

kotlin
// Extension functions and properties, null safety, safe calls, elvis, smart casts, lazy.

fun Int.isEven(): Boolean = this % 2 == 0
fun String.shout(): String = uppercase() + "!"
fun String?.orDash(): String = this ?: "-"

val Int.squared: Int get() = this * this

class User(val name: String, val email: String?)

fun domain(u: User): String? = u.email?.substringAfter('@')

fun describe(x: Any?): String = when (x) {
    null -> "null"
    is Int -> "int ${x + 1}"            // smart cast to Int
    is String -> "string of length ${x.length}"
    else -> "something else"
}

val expensive: Int by lazy {
    println("computing...")
    42
}

fun main() {
    println(4.isEven())
    println("hey".shout())
    println(null.orDash())
    println(7.squared)

    val alice = User("alice", "alice@example.com")
    val bob = User("bob", null)
    println(domain(alice))
    println(domain(bob))
    println(bob.email ?: "no email")
    println(bob.email?.length)

    println(describe(null))
    println(describe(41))
    println(describe("kotlin"))
    println(describe(1.5))

    println("before")
    println(expensive + expensive)
}

Output:

text
true
HEY!
-
49
example.com
null
no email
null
null
int 42
string of length 6
something else
before
computing...
84

Notes ​

  • An extension function is a function whose first parameter is the receiver. Nothing is added to the class.
  • A nullable reference is the same pointer as a non-null one, with NULL for null. Nullable primitives (Int?) are boxed objects.
  • lazy is implemented in the Mini stdlib as an unsynchronized lazy value, which is enough here because the stdlib is single-threaded.

Released under the Apache License 2.0.