I have 2 similar functions:
fun f1(String key, value: JsonNullable<ByteArray?>): Modification? {
return if (value.isPresent) {
when (val rawValue = value.get()) {
null -> delete(key)
else -> bar(field, rawValue)
}
} else {
null
}
}
==============
fun f2(String key, value: JsonNullable<String?>): Modification? {
return if (value.isPresent) {
when (val rawValue = value.get()) {
null -> delete(key)
else -> bar(field, rawValue)
}
} else {
null
}
}
Is there way to replace it with the single function using generic ?
bar function is overloaded:
fun bar(field: String, value: JsonNullable<ByteArray?>)...
fun bar(field: String, value: JsonNullable<String?>)...
Following the Kotlin documentation on generics, you can write a single generic function using a type variable: