I have a function that can receive either integer ...
# android
r
I have a function that can receive either integer as a string (in case an integer will get from context.getString), in java I create two different functions whereas the one that receives integer only makes the call to context.getString and calls the function which is expecting a parameter string. In kotlin have some more elegant aprouch, or is it recommended to do likewise?
j
You can create a single function with two parameters with defaults for each. Something like this
Copy code
fun foo(resId: Int? = null, str: String? = null): String? {
if (resId != null) return context.getString(resId)
return str
}
And call it like
foo(R.string.name)
or
foo(str ="Name")
. Don’t quite think it makes sense for the case of string resources but enough to understand the concept.
👍 1