https://kotlinlang.org logo
Title
p

paulex

10/09/2018, 12:08 PM
Please, How do i force a function to use its default... priorityFilter is null i want it to use the default value
e

Egor Trutenko

10/09/2018, 12:16 PM
You basically don't, there's no such opportunity in Kotlin. Instead, you can pass default value from the outside:
fetchData(0, statusFilter, priorityFilter ?: array("4"))
or check it for nullability inside the function (of course you'll also need to make the parameter nullable):
private fun fetchData(offfset ..., priority: Array<String>?) {
    val actualPriority = priority ?: array("4")
    // use actualPriority in the rest of the function
}
However, if you want to keep your function intact, I may also suggest the following:
priorityFilter?.let { fetchData(0, statusFilter, it) } ?: fetchData(0, statusFilter)
👍 1
a

Allan Wang

10/09/2018, 12:53 PM
To add onto this, defaults are only used when you don’t pass that parameter. If you are passing something nullable to a field that must be nonnull, then you can use
?:
as shown above
👍 1
r

rkeazor

10/10/2018, 12:37 PM
the best thing to do. is to create another variable storing the default , and than do a if check lol