kotlin - passing an existing function as a parameter

When we create a function we generally allow user to pass in parameter. Sometimes this parameter can be a simple integer or it can be a function. In this implementation we are going to look at passing it as a function. 

This is our sample function 

fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int

    return operation(a, b) // Call it like a regular function 

}

In order to pass in our function, we use the :: (double colon) operator 

fun existing_calculate(x: Int, y: Int) = x + y 

val result = calculate(10, 5, ::existing_calculate)






Comments

Popular posts from this blog

vllm : Failed to infer device type

android studio kotlin source is null error

gemini cli getting file not defined error