Search code examples
kotlindefault-parameters

Is there a way to distinguish between a function argument's default value having been passed explicitly or implicitly in Kotlin?


Assuming a kotlin function like this:

fun f(p1: T1? = null, p2: T2? = null, ..., pN: TN? = null) {
    // ...
}

Can the above function's implementation distinguish between the following two calls, where the first one passed p1 = null implicitly, and the second one passed it explicitly?

f()          // Implicit
f(null)      // Explicit
f(p1 = null) // Explicit

Note: There could be arbitrary numbers of parameters


Solution

  • No, it cannot distinguish between those cases.

    You could distinguish between them if you added a distinct overload, however.