Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Kotlin invokes wrong overloaded java method with variable number of arguments

I have two method in java:

Object get(A a)
Object get(A a, B... b)

and when I'm trying to invoke first method in Kotlin

get(someInstance)

It always invokes second method with empty second parameter.

How could I call first method from Kotlin in this case?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

First of all, this doesn't happen when methods are defined in Kotlin:

class A
class B

fun f(a: A) { println("one") }
fun f(a: A, vararg rest: B) { println("many") }

fun main(args: Array<String>) {
    f(A())
}

prints one. Searching on https://youtrack.jetbrains.com/issues?q=kotlin%20vararg%20java I can't find this exact issue (https://youtrack.jetbrains.com/issue/KT-11150 is close, but it has get(Object a) as the non-vararg overload). So I suggest you post it there if you can reproduce it.

Two possible workarounds:

  1. trying to adapt a trick from Kotlin function overloading (varargs vs single parameter):

    val a: A = ...
    a.let(::get)
    
  2. define a wrapper in Java:

    Object getNonVararg(A a) { return get(a); } 
    

and call it from Kotlin.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!