Writing a higher-order function using the function type as a parameter allows you to use both built-in operators and lambda expressions for the operation, so it will look like this:
fun evaluate(first: Int?, second: Int?, op: (Int, Int) -> Int): Int {
return op(first ?: 0, second ?: 0)
}
, :
val r1 = evaluate(value1, value2, Int::times)
val r2 = evaluate(value1, value2, Int::plus)
val r3 = evaluate(value1, value2, Int::minus)
val r4 = evaluate(value1, value2, Int::div)
:
val r5 = evaluate(value1, value2) { a, b -> (a * a) + b }
, v:
val v: (Int, Int)->Int = Int::times // typing needed on left to avoid ambiguous alternatives
// and then later...
val r6 = evaluate(value1, value2, v)
, , Int.(Int)->Int, , (Int, Int)->Int, this .