Skip to workspace
Kotlin Lab

Everyday Kotlin / Chapter 06

Give a retry policy defaults

Express a dependent default once and let callers override just the decision they own.

The decision

Build a retry schedule in seconds. The first delay is the base capped at the limit. Each subsequent delay doubles up to the cap. Default to three attempts and a cap of twice the supplied base.

Read the Kotlin

Fill in the missing parameters first

At the call site, bind each supplied argument to its parameter. Evaluate an omitted default using those bound values. With base 2 the omitted cap becomes 4. The List initializer emits the current delay once per slot, then the next slot sees the doubled value limited by that cap.

delays(base = 2, attempts = 4)  // [2, 4, 4, 4]

Treat a parameter name as part of the Kotlin source API

Positional arguments bind by position. Named arguments bind by the spelling in the declaration. Renaming a parameter can leave the JVM signature intact yet break a Kotlin caller on its next compilation. During review, search for calls that spell the old name, including calls that also supply some arguments positionally.

delays(3, 12, 1)         // all positional
delays(3, attempts = 1)  // depends on the name attempts

Skip a middle default by naming what follows

In delays(7, 1) the second positional argument fills cap. To change only the number of attempts, name that argument. When all arguments are named, their written order can change. A count of zero runs the List initializer zero times and yields an empty schedule.

delays(attempts = 1, base = 4)  // one delay, computed cap
delays(4, 1)                    // cap 1, default attempt count

The full example

Java 25
import java.util.List;
import java.util.ArrayList;

class Lesson {
    static List<Integer> delays(int base) {
        return delays(base, base * 2, 3);
    }

    static List<Integer> delays(int base, int cap, int attempts) {
        final var values = new ArrayList<Integer>();
        var next = Math.min(base, cap);
        for (int i = 0; i < attempts; i++) {
            values.add(next);
            next = Math.min(cap, next * 2);
        }
        return List.copyOf(values);
    }
}
Kotlin/JVM
fun delays(base: Int, cap: Int = base * 2, attempts: Int = 3): List<Int> {
    var next = minOf(base, cap)
    return List(attempts) {
        val current = next
        next = minOf(cap, current * 2)
        current
    }
}

Inputs and outcomes

InputOutcomeWhy it matters
base 4, no overrides[4, 8, 8]The supplied base determines the default cap.
base 4, attempts 1[4]A named argument can skip the cap.
base 8, cap 3, attempts 2[3, 3]Even the first attempt respects the caller’s cap.

Walk through the code

  1. Kotlin can reference an earlier parameter in a default expression. The omitted cap is computed for this call using its base.
  2. A caller can write delays(4, attempts = 1) and preserve the dependent cap. Java’s short overload forwards all parameters to one implementation.
  3. List(attempts) calls its initializer once per slot, in order. Each iteration returns the current delay and updates the value for the next attempt.

The Java habit

Renaming attempts looks like an internal cleanup to a Java author. Recompiled Kotlin source that calls delays(attempts = …) needs the original parameter name.

When to choose differently

When independent numeric options accumulate units and validation rules, a policy object can make them easier to evolve. Java consumers may benefit from specific overloads.

References

Open quizTry code