Given a function with a default argument, is there...
# getting-started
a
Given a function with a default argument, is there a way to call it with its default argument? I want to write a wrapper function for it while preserving the default argument value. Basically this, but without the if
Copy code
fun foo(arg1: Int, arg2: Int = 42){ ... }

fun wrapper(arg1: Int, arg2: Int? = null){
    if (arg2 == null)
        foo(arg1)
    else
        foo(arg1, arg2)
}
s
Possible with reflection, using
callBy
, but I wouldn't recommend that as a solution here
Do you expect people to pass
null
, or is that just a placeholder? You could use overloads, if you just need a 1-arg version and a 2-arg version:
Copy code
fun wrapper(arg1: Int) = foo(arg1)
fun wrapper(arg1: Int, arg2: Int) = foo(arg1, arg2)
e
a
Thanks