https://kotlinlang.org logo
#android
Title
# android
r

rCorbellini

04/05/2018, 12:24 PM
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

jishindev

04/06/2018, 5:55 AM
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
2 Views