Skip to content

Instantly share code, notes, and snippets.

@ciferkey
Last active October 14, 2018 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ciferkey/3ba7ce78ac9a77d7fcbb2e505c1cbd3a to your computer and use it in GitHub Desktop.
Save ciferkey/3ba7ce78ac9a77d7fcbb2e505c1cbd3a to your computer and use it in GitHub Desktop.
// 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