Skip to content

Instantly share code, notes, and snippets.

@nomisRev
Last active September 23, 2017 08:55
Show Gist options
  • Save nomisRev/366a4c2c8d421d3359f910d4eb682dd5 to your computer and use it in GitHub Desktop.
Save nomisRev/366a4c2c8d421d3359f910d4eb682dd5 to your computer and use it in GitHub Desktop.
Demo default params and the effect on calling them
fun main(args: Array<String>) {
fun higherOrder(i: Int = 0, f: (Int) -> Int): Int = f(i)
fun higherOrder2(f: (Int) -> Int, i: Int = 0): Int = f(i)
higherOrder { it + 1 }.let(::println)
higherOrder2({ it + 1 }).let(::println) //forced to use () in order to omit default param
fun value(value: Int, df: Int = 0) = value
fun value2(df: Int = 0, value: Int) = value
value(1).let(::println)
value2(value = 1).let(::println) //forced to use named param in order to skip default param
//as shown above after value and before higher order
fun valueAndHigher(value: Int, i: Int = 0, f: (Int) -> Int): Int = f(value)
valueAndHigher(2) { it + 1 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment