This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* While calling the higher order function When the function type parameter is one and only parameter, | |
* and if the function type has also one and only parameter, | |
* in addition to function call parentheses, we can also omit the parameter of the function type and an arrow ```->``` | |
* We just write core business logic between curly braces | |
* and we can access our single parameter through the keyword it like below: | |
*/ | |
doSomething { "$it" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//region A function type that has only one parameter | |
private fun doSomething(ft: (Int) -> String) { | |
/*...*/ | |
} | |
//endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//region A higher order function that has one and only function type parameter | |
private fun doSomething(ft: (Int, Int) -> Int) { | |
val result = ft(1, 2) | |
/*...*/ | |
} | |
//endregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* While calling the higher order function When the function type parameter is only parameter, | |
* we can simply write our business logic between curly braces after writing the function name | |
* and omit function call parentheses like below: | |
*/ | |
doSomething { x: Int, y: Int -> x + y } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int) { | |
//passing a function type to another function | |
val funOne = someFunction(ftOne) | |
/*...*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun Int.doSomething(y: Int, noinline ftOne: Int.(Int) -> Int) { | |
//passing a function type to another function | |
val funOne = someFunction(ftOne) | |
/*...*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun Int.doSomething(y: Int, noinline ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) { | |
//passing a function type to another function | |
val funOne = someFunction(ftOne) | |
/*...*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Compile time error… Illegal usage of inline function type [ftOne]... | |
inline fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) { | |
//passing a function type to another function | |
val funOne = someFunction(ftOne) | |
/*...*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) { | |
/*...*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Int.doSomething(y: Int, ftOne: Int.(Int) -> Int, ftTwo: (Int) -> Int) { | |
/*...*/ | |
} |