Please, How do i force a function to use its defau...
# android
p
Please, How do i force a function to use its default... priorityFilter is null i want it to use the default value
e
You basically don't, there's no such opportunity in Kotlin. Instead, you can pass default value from the outside:
Copy code
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):
Copy code
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:
Copy code
priorityFilter?.let { fetchData(0, statusFilter, it) } ?: fetchData(0, statusFilter)
👍 1
a
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
the best thing to do. is to create another variable storing the default , and than do a if check lol