I have a kotlin code/library that will end up being used in Java, one of the methods there is:
@JvmOverloads
fun sample(
param1: ByteArray,
param2: ByteArray = byteArrayOf(),
param3: ByteArray = customMethod()
): ByteArray { ... }
Since the method is overloaded (@JvmOverloads), how do I use it in java with only sample(param1, param3), excluding param2?
Java detects the second parameter in sample(param1, param3) as param2, rather than param3 considering that they are of the same data type.
Do I really have to resort to nulls or perhaps an empty byte[] just to skip over the part?
I have tried:
- Resorting to
nulls and do checks based on that, so now I havesample(param1, null, param2);. - Using empty
byte[]to skip over the part, so now I havesample(param1, byte[], param2);.
But, I want/prefer to have param2 undefined/skipped rather than being defined at all.
What I have researched (but haven't tried)
Data classes as parameters(?)
fun sample(param: DataClass): ByteArray { ... }This code:
Foo foo = new Foo() {{ color = red; name = "Fred"; size = 42; }};But I don't think they are desirable (last resort maybe?)