liveBook · Manning

Default value

As explained in Kotlin in Action, 2E

A default parameter is a function parameter with a value automatically supplied when the caller omits that argument.

A default parameter value lets Kotlin supply an argument automatically when a caller omits it. This reduces overloads, repeated signatures, and uncertainty about omitted arguments. section 3.2.2

Optional formatting arguments

fun <T> joinToString( collection: Collection<T>, separator: String = ", ", prefix: String = "", postfix: String = "" ): String { /* ... */ } joinToString(list) // 1, 2, 3 joinToString(list, "; ") // 1; 2; 3 joinToString(list, postfix = ";", prefix = "# ") // # 1, 2, 3; Regular calls follow declaration order and omit only trailing arguments. Named arguments can omit middle parameters and supply selected arguments in any order. section 3.2.2

Listing 3.2 Declaring joinToString() with default parameter values
fun <T> joinToString(
        collection: Collection<T>,
        separator: String = ", ",    #1
        prefix: String = "",         #1
        postfix: String = ""         #1
): String { /* ... */ }

Inspect the complete joinToString declaration with comma, empty-prefix, and empty-postfix defaults. section 3.2.2

Defaults can also be function values. A collection formatter can default transform to { it.toString() }, while callers may omit it, pass a trailing lambda, or replace it with a named argument. section 10.1.4

Listing 10.5 Specifying a default value for a parameter of a function type
fun <T> Collection<T>.joinToString(
        separator: String = ", ",
        prefix: String = "",
        postfix: String = "",
        transform: (T) -> String = { it.toString() }         #1
): String {
    val result = StringBuilder(prefix)
 
    for ((index, element) in this.withIndex()) {
        if (index > 0) result.append(separator)
        result.append(transform(element))                    #2
    }
 
    result.append(postfix)
    return result.toString()
}
 
fun main() {
    val letters = listOf("Alpha", "Beta")
    println(letters.joinToString())                          #3
    // Alpha, Beta
    println(letters.joinToString { it.lowercase() })         #4
    // alpha, beta
    println(letters.joinToString(separator = "! ", postfix = "! ",
           transform = { it.uppercase() }))                  #5
    // ALPHA! BETA!
}

Compare the default transform lambda with calls that use the default conversion, a trailing lambda, or an explicit named transform. section 10.1.4

The default is encoded in the called function rather than copied into each call site. Recompiling the function with a changed default updates callers that omit that argument. Java has no equivalent syntax: Java callers must provide every value unless @JvmOverloads generates overloads for omitted trailing parameters. section 3.2.2

How Kotlin in Action, 2E covers it

How deep it goes
Explains declaration syntax, positional and named calls, function-type defaults, recompilation behavior, and Java overload generation.
Examples
Uses joinToString formatting options, a default transform lambda, and a Person age default in an introductory collection example.
What it assumes
Assumes basic Kotlin function declarations and calls while introducing named arguments, default syntax, and Kotlin-to-Java calling constraints.
What you can do after
Apply it in Kotlin to replace routine overloads and design callable APIs with optional positional, named, or function-valued arguments.
How it connects
Connects default values with named arguments, function types, lambdas, overloaded functions, and Java interoperability.

Also explained in

Compare how 20 books explain default value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.