使用Java中相同数据类型的Kotlin重载方法参数

huangapple go评论81阅读模式
英文:

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 have sample(param1, null, param2);.
  • Using empty byte[] to skip over the part, so now I have sample(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)

答案1

得分: 4

无法。@JvmOverloads 只生成从第一个参数到最后一个参数的重载版本。所以你可以使用 param1param2,但不能只使用 param1param3

你可以交换参数的顺序,但这只在你不需要在其他地方使用 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).

huangapple
  • 本文由 发表于 2023年6月19日 19:40:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定