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?>)...
I think you are thinking about this incorrectly String does not inherit from ByteArray (or vice versa) and they don't inherit from any base class except Object.
So unless you just want it to be anything that extends Object (Both String an ByteArray do extend object) you will need to use generics.
I think this example will give you a basic understanding
https://pl.kotl.in/umls8tYpV
You use a generic and then use an if or switch statement to do something that is specific per type.
In the case of using an upper bounds with Object it would be these
So if this doesn't answer your question you should probably reword it to make it a little more precise about what you are looking for.
Fair Warning... Java/Scala Developer do very little Kotlin so some stuff may be overly verbose
Also if you want to ONLY accept ByteArray or String you can do this...