Call java varargs method from kotlin

TpoM6oH

I have a java function:

public static void initialize(@NonNull Activity activity, Settings... settings) {}

I want to call it from kotlin:

fun initialize(activity: Activity, vararg settings: settings) = JavaClass.initialize(activity, settings)

But it does not compile, telling me that there is type mismatch, Settings is required, but the argument is kotlin.Array<out Settings>

I see that it's trying to match it with signture

public static void initialize(@NonNull Activity activity, Settings settings) {}

but I want to use

public static void initialize(@NonNull Activity activity, Settings[] settings) {}
Michael

You should use the following syntax:

fun initialize(activity: Activity, vararg settings: settings) =
    JavaClass.initialize(activity, *settings)

https://kotlinlang.org/docs/reference/java-interop.html#java-varargs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a generic Java varargs method from Kotlin

From Dev

Kotlin -> Java API call is not using the method I want because varargs method is swallowing all method arguments

From Java

How to call a varargs method with an additional argument from a varargs method

From Dev

call method from java fragment to kotlin fragment?

From Dev

Overriding a Java @Nullable varargs method in Kotlin, IDE complains it overrides nothing

From Java

Have Java method call deal with varargs passed in on program execution

From Java

Call Kotlin object with class delegation from Java as a static method

From Dev

How to call from Java Kotlin method which is named with escape characters?

From Java

From Kotlin call Java method requiring Function parameter

From Dev

How to call a kotlin object method(in aar) from java?

From Java

Varargs Java Ambiguous Call

From Dev

Call method from Kotlin class

From Java

Varargs in method overloading in Java

From Java

Java method overloading and varargs

From Dev

Method call is ambigous with varargs operator

From Java

Convert Kotlin Array to Java varargs

From Java

java warning: Varargs method could cause heap pollution from non-reifiable varargs parameter

From Dev

Kotlin - Call fragment method from MainActivity

From Java

Recursive method call cause StackOverFlowError in kotlin but not in java

From Java

Kotlin call java method with Class<T> argument

From Java

Possibility to call a Java static method in Kotlin

From Java

Java two varargs in one method

From Dev

Call from Kotlin a Java generic method defined with multiple bounds, one bound is itself a generic type

From Dev

How do I call a Java method that takes a Non-null Void parameter from Kotlin?

From Dev

How can i know when calling a java method from Kotlin if a nullable argument should be checked at call site?

From Dev

Call Method From interface in Java

From Java

Call Java method from .Net

From Java

Kotlin-Java interop not working with varargs

From Dev

Varargs Kotlin Java interop not working properly

Related Related

  1. 1

    Calling a generic Java varargs method from Kotlin

  2. 2

    Kotlin -> Java API call is not using the method I want because varargs method is swallowing all method arguments

  3. 3

    How to call a varargs method with an additional argument from a varargs method

  4. 4

    call method from java fragment to kotlin fragment?

  5. 5

    Overriding a Java @Nullable varargs method in Kotlin, IDE complains it overrides nothing

  6. 6

    Have Java method call deal with varargs passed in on program execution

  7. 7

    Call Kotlin object with class delegation from Java as a static method

  8. 8

    How to call from Java Kotlin method which is named with escape characters?

  9. 9

    From Kotlin call Java method requiring Function parameter

  10. 10

    How to call a kotlin object method(in aar) from java?

  11. 11

    Varargs Java Ambiguous Call

  12. 12

    Call method from Kotlin class

  13. 13

    Varargs in method overloading in Java

  14. 14

    Java method overloading and varargs

  15. 15

    Method call is ambigous with varargs operator

  16. 16

    Convert Kotlin Array to Java varargs

  17. 17

    java warning: Varargs method could cause heap pollution from non-reifiable varargs parameter

  18. 18

    Kotlin - Call fragment method from MainActivity

  19. 19

    Recursive method call cause StackOverFlowError in kotlin but not in java

  20. 20

    Kotlin call java method with Class<T> argument

  21. 21

    Possibility to call a Java static method in Kotlin

  22. 22

    Java two varargs in one method

  23. 23

    Call from Kotlin a Java generic method defined with multiple bounds, one bound is itself a generic type

  24. 24

    How do I call a Java method that takes a Non-null Void parameter from Kotlin?

  25. 25

    How can i know when calling a java method from Kotlin if a nullable argument should be checked at call site?

  26. 26

    Call Method From interface in Java

  27. 27

    Call Java method from .Net

  28. 28

    Kotlin-Java interop not working with varargs

  29. 29

    Varargs Kotlin Java interop not working properly

HotTag

Archive