Kaj Koivunen
01/18/2023, 2:24 PMvar defaultValue = 4
fun foo(number: Int = defaultValue) { /*something*/ }
even when I change the var, it appears to call the function with the new value. Is there any pitfalls of doing this that I'm unaware of or is it perfectly fine?Sam
01/18/2023, 2:30 PMdefault argument expressions which are used (i.e., for which the call-site does not provide explicit arguments) are reevaluated at every such call-site. Default argument expressions which are not used (i.e., for which the call-site provides explicit arguments) are not evaluated at such call-sites.https://github.com/Kotlin/kotlin-spec/blob/release/docs/src/md/kotlin.core/expressions.md
Kaj Koivunen
01/18/2023, 2:33 PMCLOVIS
01/18/2023, 3:36 PMephemient
01/18/2023, 6:44 PMfun foo(number: Int = defaultValue)
is similar to writing
fun foo() = foo(number = defaultValue)
fun foo(number: Int)
ephemient
01/18/2023, 6:45 PM@JvmOverloads
) but that's conceptually what happens)