英文:
Using kotlin overloaded method parameters with same data type in Java
问题
// To call the Kotlin method `sample(param1, param3)` in Java and exclude `param2`, you can use named arguments:
byte[] result = MyClassKt.sample(param1, new byte[0], param3);
In the above code, we explicitly provide an empty byte[] for param2 to exclude it, and we pass param3 as the desired argument.
The other approaches you mentioned, such as using null or an empty byte[], are also valid but may not achieve the exact behavior you desire, as they involve defining param2. Using named arguments as shown above provides a clear and explicit way to skip param2 without defining it.
英文:
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; }};> from: https://stackoverflow.com/a/1988268/16171990
But I don't think they are desirable (last resort maybe?)
答案1
得分: 4
无法。@JvmOverloads 只生成从第一个参数到最后一个参数的重载版本。所以你可以使用 param1 和 param2,但不能只使用 param1 和 param3。
你可以交换参数的顺序,但这只在你不需要在其他地方使用 param1 和 param2 版本时才有效。或者,你可以手动添加一个只接受 param1 和 param3 的重载版本,并从 Java 中调用它:
fun sample2(param1: ByteArray, param3: ByteArray) = sample(param1, param3 = param3)
这个版本应该有一个不同的名称(或者你可能会与 param1 和 param2 版本混淆),而且它不需要 param3 的默认值(因为它只使用原始方法)。
英文:
> Since the method is overloaded (@JvmOverloads), how do I use it in java with only sample(param1, param3), excluding param2?
You can't. @JvmOverloads only generates overloads with parameters from first to last. So you can call with param1 and param2, but not with param1 and param3.
You could swap the order of parameters around, but that only works if you don't also need the param 1 & 2 version somewhere else. Or, you could manually add an overload that only takes 1 & 3 and call that from java:
fun sample2(param1: ByteArray, param3: ByteArray) = sample(param1, param3 = param3)
This one should have a different name (or you could confuse with with the param 1 & 2 version), and it doesn't need the default for param3 (because just use the original method).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论