Last active
October 14, 2018 00:04
-
-
Save ciferkey/3ba7ce78ac9a77d7fcbb2e505c1cbd3a to your computer and use it in GitHub Desktop.
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
// In Kotlin function definitions start with the keywork "fun" | |
// Additionally Kotlin lets you have "top level" functions that do not need to be inside a class | |
// "(Workbook) -> Unit" means "a function that takes a workbook and returns nothing (Unit) | |
fun workbook(block: (Workbook) -> Unit): Workbook { | |
var wb = HSSFWorkbook() | |
// Note that in Kotlin we can direcly call "block" rather than needing to call "accept" on it | |
block(wb) | |
return wb | |
} | |
fun tryItOut() { | |
workbook({wb -> | |
wb.setHidden(true) | |
wb.setSelectedTab(2) | |
}) | |
// In Kotlin you can drop the lambda parameters name and use the default name "it" | |
workbook({ | |
it.setHidden(true) | |
it.setSelectedTab(2) | |
}) | |
// In Kotlin if the last parameter of a method is a lambda then you can move it outside the parenthesis | |
workbook() { | |
it.setHidden(true) | |
it.setSelectedTab(2) | |
} | |
// Also since the lambda is the only parameter we can even drop the parenthesis all together! | |
workbook { | |
it.setHidden(true) | |
it.setSelectedTab(2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment