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.
fun add = first?:0 + second?:0
. Only difference being that it wont return null when both are null. – Mylofun add(first : Int?, second : Int?) : Int? = first?:0 + second?:0
– Interstadialfun add():Int = (first:?0) + (second:?0)
works for me. – Mylo