Hi guys, ```fun foo(s: String = "") = "hello $s" ...
# announcements
j
Hi guys,
Copy code
fun foo(s: String = "") = "hello $s"

fun bar(a: String?) {
   foo(a ?: "")
}
In the above code is there a way for us to use the default value for the parameter in case
a
is null, instead of repeating the default value in
bar
? This way, we don’t rely on what the default value of
foo
is within
bar
s
Copy code
fun foo(s: String? = null) = "hello ${s ?: ""}"
j
Thats fair, except that the example above is a simplification of code where
foo
and
bar
couldn’t be coupled like this.
s
If you have additional requirements then please state them. You could also do:
Copy code
fun foo(s: String = "") = "hello $s"

fun bar(a: String?) {
   if (a != null) foo(a) else foo()
}