Kotlin Compilation Error : None of the following functions can be called with the arguments supplied

Viewed 53774

I have a class whose constructor takes 2 int parameters (null values are allowed). Following is the compilation error.

None of the following functions can be called with the arguments supplied: 
public final operator fun plus(other: Byte): Int defined in kotlin.Int
public final operator fun plus(other: Double): Double defined in kotlin.Int
public final operator fun plus(other: Float): Float defined in kotlin.Int
public final operator fun plus(other: Int): Int defined in kotlin.Int
public final operator fun plus(other: Long): Long defined in kotlin.Int
public final operator fun plus(other: Short): Int defined in kotlin.Int

Here is the NumberAdder class.

class NumberAdder (num1 : Int?, num2 : Int?) {

    var first : Int? = null
    var second : Int? = null

    init{
    first = num1
    second = num2
    }

fun add() : Int?{

    if(first != null && second != null){
        return first + second
    }

    if(first == null){
        return second
    }

    if(second == null){
        return first
    }

    return null
}

}

How can I resolve this issue? I want to return null if both are null. If one of them is null, return the other, and otherwise return the sum.

4 Answers

I experienced a similar issue with assertEquals.

My code was

assertEquals(
       expeted = 42, // notice the missing c 
       actual = foo()
)

After I've fixed the typo, my IDE said I can't use named arguments with non-Kotlin functions, so I extracted the values to variables and everything started working as it should.

 val expected = 42
 val actual = foo()
 assertEquals(expected, actual)
Related