Reputation: 8410
I have some callback that has a parameter defined like this:
val getMoreFunction : (() -> FSDirResult)? = null
Nullable, because I need to know if the function is actually defined. But I feel it's bad. I'd rather pass {}
instead of null
to my callbacks. So is there a way to determine if empty function was passed?
Upvotes: 3
Views: 3089
Reputation: 41678
Ideally you would have a default parameter that can be called i.e.:
fun saneDefault(getMoreFunction: (() -> String) = { "" }): Boolean {
val result = getMoreFunction()
return result.isEmpty()
}
However if you really, really, really need to know if the argument was passed you can store the default parameter value like so:
private val DEFAULT = { throw UnsupportedOperationException("this shouldn't be called") }
fun isDefaultPassed(getMoreFunction: (() -> String) = DEFAULT): Boolean {
if(getMoreFunction == DEFAULT){
return true
}
return false
}
Upvotes: 4